#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include using namespace std; const ll INF=1LL<<60; struct Edge{ int to; ll w; Edge(int to,ll w) : to(to),w(w) {} }; using Graph=vector>; using pli=pair; template bool chmin(T& a,T b){ if(a>b){ a=b; return true; } return false; } int main(){ int t,n,m; cin>>t>>n>>m; Graph G(n); if(t==0){ rep(i,m){ int u,v,w; cin>>u>>v>>w; u--,v--; G[u].push_back(Edge(v,w)); G[v].push_back(Edge(u,w)); } ll mi=INF; for(int i=0;i dist(n,INF); dist[i]=0; priority_queue,greater> que; que.push({dist[i],i}); while(!que.empty()){ int v=que.top().second; ll d=que.top().first; que.pop(); if(d>dist[v]) continue; for(auto e:G[v]){ if(chmin(dist[e.to],dist[v]+e.w)){ que.push({dist[e.to],e.to}); } } } mi=min(mi,dist[e.to]+e.w); G[i].insert(G[i].begin()+j,e); } } if(mi==INF) cout<<-1<>u>>v>>w; u--,v--; G[u].push_back(Edge(v,w)); } ll mi=INF; for(int i=0;i dist(n,INF); dist[e.to]=0; priority_queue,greater> que; que.push({dist[e.to],e.to}); while(!que.empty()){ int v=que.top().second; ll d=que.top().first; que.pop(); if(d>dist[v]) continue; for(auto e:G[v]){ if(chmin(dist[e.to],dist[v]+e.w)){ que.push({dist[e.to],e.to}); } } } mi=min(mi,dist[i]+e.w); G[i].insert(G[i].begin()+j,e); } } if(mi==INF) cout<<-1<