using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; //using nint=System.Int32; static class Program{ const int mod=(int)1e9+7; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; var h=new int[s[1]][]; var bh=new long[s[1]]; var li=new List[s[0]+1]; const long inf=long.MaxValue>>1; for(int i = 0;i(); c[i]=inf; } Array.Sort(h,(u,v)=>u[1]-v[1]); for(int i = 0;i(32); pq.Push(new Rheap.Dt(0,1)); while(pq.cnt>0){ var e=pq.Dequeue; if(b[e.d]){continue;} b[e.d]=true; for(int i = 0;ie.n+((e.d+h[p][j]+1)>>1)+h[p][1]){ c[h[p][j]]=e.n+((e.d+h[p][j]+1)>>1)+h[p][1]; pq.Push(new Rheap.Dt(c[h[p][j]],h[p][j])); } } bh[p]=e.n; } } Console.WriteLine("{0}",c[s[0]]==inf?-1:c[s[0]]); } } public class Rheap{ public class Dt{ public long n; public T d; public Dt(long n,T d){this.n=n;this.d=d;} public override string ToString()=>"d:"+d.ToString()+" n:"+n.ToString(); } private const int m=65; private long la=0; private Dt[][] bf; private int[] h; public int cnt=0; public Rheap(int max){ bf=new Dt[m][]; h=new int[m]; for(int i = 0;i0){ int p=0; for(int j = 1;jbf[i][j].n){p=j;} } la=bf[i][p].n; for(int j = 0;j(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]));} public (T1,T2) Tp2(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]));} public (T,T,T) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} public (T1,T1,T2) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} private T Ct(string s){return (T)Convert.ChangeType(s,typeof(T));} }