/*
TASK: seq
LANG: C++
*/
#include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;

 map < int,pair<int,int> > g;
 map < int,pair<int,int> >::iterator it,s;
 int a[1000002];
 int n;

 int r (int i,int j,int k)
  {
   return (i>j && j<k) || (i<j && j>k);
  }  

 int main ()
  {
   int i,c1,c2,o1,o2,x1,y1,x2,y2,p,q;
   scanf("%d",&n);
   for (i=1;i<=n;i++)
    {
     scanf("%d",&a[i]);
     g[a[i]].first=g[a[i]].second=0;
    }
   for (i=1;i<=n;i++)
    {
     scanf("%d",&a[i]);
     if (g[a[i]].first==0) g[a[i]].first=i;
     g[a[i]].second=i;
    }
   c1=c2=0;
   for (it=g.begin();;it++)
    {
     s=it;
     s++;
     if (s==g.end()) break;
     x1=it->second.first;
     y1=it->second.second;
     x2=s->second.first;
     y2=s->second.second;
     p=r(y1,x1,y2);
     q=r(x1,y1,y2);
     o1=min(c1+r(x1,y2,x2)+p,c2+r(y1,y2,x2)+q);
     p=r(y1,x1,x2);
     q=r(x1,y1,x2);
     o2=min(c1+r(x1,x2,y2)+p,c2+r(y1,x2,y2)+q);
     c1=o1;
     c2=o2;
    }
   printf("%d\n",min(c1,c2));
   return 0;
  }
