import java.io.*; import java.util.*; class P implements Comparable
{
long a,b;
P(long x,long y){a=x;b=y;}
@Override
public int compareTo(P o){
return Long.compare(a,o.a);
}
}
class Main {
static final long I=1L<<53;
static Map ();
q.add(new P(0,si));
while(q.size()>0){
P dw=q.poll();
long d=dw.a;
int v=(int)dw.b;
if(dis[v]<=d)continue;
dis[v]=d;
for(long e:g[v]){
int w=(int)(e>>>32);
long nd=d+(int)e;
if(dis[w]<=nd)continue;
q.add(new P(nd,w));
}
}
out.println(dis[ti]==I?-1:dis[ti]);
out.close();
}
// http://codeforces.com/blog/entry/7018
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}