#include <iostream>
#include <vector>
using namespace std;
struct edge {
int v;
int w;
};
int n, m;
int ans = -1;
vector<edge> e[50005];
int in[1505] = { 0 };
bool vis[1505] = { 0 };
void dfs(int now, int sum) {
if (now == n) {
ans = max(ans, sum);
return;
}
while (e[now].empty() == false) {
edge ed;
ed = e[now].back();
e[now].pop_back();
if (vis[ed.v] == true)
continue;
vis[ed.v] = true;
dfs(ed.v, sum + ed.w);
vis[ed.v] = false;
}
}
void addedge(int x, int y, int len) {
edge now;
in[y]++;
now.v = y;
now.w = len;
e[x].push_back(now);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
addedge(a, b, c);
}
if (in[n] == 0) {
cout << ans;
return 0;
}
vis[1] = 1;
dfs(1, 0);
cout << ans;
return 0;
}