#include<bits/stdc++.h>
using namespace std ;
const int N = 1e5 + 500 ;
int n , m ;
int x , y , z ;
struct egde{
int to , w ;
};
int dis[N] ;
vector<egde> tu[N] ;
bool used[N] ;
void spfa(int x){
deque<int> dq ;
memset(dis , 0x3f , sizeof(dis)) ;
dis[x] = 0 ;
dq.push_back(x) ;
used[x] = 1 ;
while(!dq.empty()){
int now = dq.front() ;
dq.pop_front() ;
used[now] = 0 ;
for(int i = 0 ; i < tu[now].size() ; i++){
int now_w = tu[now][i].w , now_to = tu[now][i].to ;
if(dis[now] + now_w < dis[now_to]){
dis[now_to] = dis[now] + now_w ;
if(!used[now_to]){
if(dis[now_to] < dis[dq.front()]) dq.push_front(now_to) ;
else dq.push_back(now_to) ;
used[now_to] = 1 ;
}
}
}
}
}
int main(){
scanf("%d%d" , &n , &m) ;
for(int i = 1 ; i <= m ; i++){
scanf("%d%d%d" , &x , &y , &z) ;
tu[x].push_back({y , z}) ;
}
spfa(1) ;
if(dis[n] > 1e9 / 2){
printf("impossible\n") ;
}else{
printf("%d\n" , dis[n]) ;
}
return 0 ;
}