#include <iostream>
#include <cstring>
using namespace std;
const int N = 550, M = 100010;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool state[N];
int n, m;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void Dijkstra() {
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
if (!state[j] && (t == -1 || (dist[j] < dist[t]))) {
t = j;
}
}
state[t] = true;
for (int j = h[t]; j != -1; j = ne[j]) {
int u = e[j];
// printf("t:%d\n", t);
// printf("dist[%d]:%d dist[t] + w[j]:%d\n", u, dist[u], dist[t] + w[j]);
dist[u] = min(dist[u], dist[t] + w[j]);
// printf("校正后:dist[%d] = %d\n", u, dist[u]);
}
}
}
int main(void)
{
memset(h, -1, sizeof(h));
cin >> n >> m;
while (m--) {
int a, b, w;
cin >> a >> b >> w;
add(a, b, w);
}
Dijkstra();
// printf("n = %d\n", n);
if (dist[n] != 0x3f3f3f3f) cout << dist[n] << endl;
else cout << "-1" << endl;
return 0;
}