#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N, INF = 0x3f3f3f3f;
int n,m;
int h[N], e[M], w[N], ne[M], idx;
int dist[N], q[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
int hh = 0, tt = 0;
q[tt ++ ] = 1;
dist[1] = 0;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
return dist[n];
}
int main()
{
cin >> n >> m;
memset(h,-1, sizeof h);
while(m--)
{
int a,b, c;
cin >> a >> b >> c;
add(a,b,c);
}
int res = spfa();
if(res == INF)puts("impossible");
else cout << res;
}