/*
* data : 2021/ 11 / 25
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
int idx, n, m, e[N], ne[N], w[N], h[N], dist[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++;
}
void spfa(){
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q; q.push(1);
st[1] = true;
while(!q.empty()){
int fa = q.front(); q.pop();
st[fa] = false;
for(int i = h[fa]; ~i; i = ne[i]) {
int son = e[i];
if(dist[son] > dist[fa] + w[i]) {
dist[son] = dist[fa] + w[i];
q.push(son); st[son] = true;
}
}
}
if(dist[n] > INF / 2) puts("impossible");
else printf("%d\n", dist[n]);
}
int main(void) {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for(int i = 1; i <= m; i++) {
int a, b, c; scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
spfa();
return 0;
}