#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 150000;
int h[N], w[N], e[N], ne[N], idx, m, n, dist[N], st[N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int dijstra()
{
dist[1] = 0;
q.push({0, 1});
while (q.size())
{
auto t = q.top();
q.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = 1;
for (auto i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
}
q.push({dist[j], j});
}
}
if(dist[m] == 0x3f3f3f3f) return -1;
return dist[m];
}
int main()
{
memset(dist, 0x3f, sizeof dist);
memset(h, -1, sizeof h);
cin >> m >> n;
int a, b, c;
while (n --)
{
cin >> a >> b >> c;
add(a, b, c);
}
cout << dijstra() << endl;
return 0;
}