import java.util.*;
public class SPFA模板 {
static int N = 1005;
static int len;
static int[] head = new int[N];
static edge[] e = new edge[N];
static int[] dis = new int[N];
static boolean[] inq = new boolean[N];
static int[] cntInq = new int[N];
static void add(int u, int v, int w) {
e[len] = new edge(v, w, head[u]);
head[u] = len++;
}
static void add2(int u, int v, int w) {
add(u, v, w);
add(v, u, w);
}
static void init() {
Arrays.fill(head, -1);
Arrays.fill(dis, 0x3f3f3f3f);
}
static int n,m;
public static void main(String[] args) {
init();
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
while (m-- > 0) {
add2(sc.nextInt(), sc.nextInt(), sc.nextInt());
}
if (spfa(1)){
System.out.println("有负环");
} else {
System.out.println(dis[n]);
}
}
static boolean spfa(int u) {
dis[u] = 0;
inq[u] = true;
cntInq[u]++;
Queue<Integer> q = new LinkedList<Integer>();
q.add(u);
while (q.size() != 0) {
u = q.poll();
inq[u] = false;
for (int j = head[u]; j != -1; j = e[j].next) {
int v = e[j].v;
int w = e[j].w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
if (!inq[v]) {
cntInq[v]++;
q.add(v);
inq[v] = true;
if (cntInq[v] > n) return true;
}
}
}
}
return false;
}
}