import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class Main{ static final InputStream in=System.in; static final PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(in)); StringTokenizer st; String line=""; while((line=br.readLine())!=null&&!line.isEmpty()){ int nw=Integer.parseInt(line); st=new StringTokenizer(br.readLine()); int res,insertionIndex; int[] w=new int[nw]; for(int i=0;i=0)?res:~res; if(insertionIndex==0||b[insertionIndex-1]==b[0]) break; prev=b[insertionIndex-1]; ans1++; res=Arrays.binarySearch(w,prev); insertionIndex=(res>=0)?res:~res; if(insertionIndex==0||w[insertionIndex-1]==w[0]) break; prev=w[insertionIndex-1]; ans1++; } prev=b[nb-1]; while(true){ res=Arrays.binarySearch(w,prev); insertionIndex=(res>=0)?res:~res; if(insertionIndex==0||w[insertionIndex-1]==w[0]) break; prev=w[insertionIndex-1]; ans2++; res=Arrays.binarySearch(b,prev); insertionIndex=(res>=0)?res:~res; if(insertionIndex==0||b[insertionIndex-1]==b[0]) break; prev=b[insertionIndex-1]; ans2++; } out.println(Math.max(ans1,ans2)); } out.flush(); } }