program xqz; const maxn=50000; var ans,i,j,k,m,n,k1,k2,t1,t2:longint; f,fa,s,x,y,re:array[0..maxn] of longint; pos,o:array[0..100000] of longint; a:array[0..maxn,0..2] of longint; function find(i:longint):longint; var kk:longint; begin if fa[i]<>i then begin kk:=find(fa[i]); re[i]:=re[i] xor re[fa[i]]; fa[i]:=kk; end; find:=fa[i]; end; procedure opp(i:longint); begin ans:=ans-2*f[i]+s[i]; re[i]:=re[i] xor 1; f[i]:=s[i]-f[i]; end; procedure link(i,j:longint); begin fa[j]:=i; inc(s[i],s[j]); inc(f[i],f[j]); end; begin read(n); for i:=1 to n do read(x[i]); for i:=1 to n do read(y[i]); for i:=1 to n do begin fa[i]:=i; s[i]:=1; k1:=find(pos[x[i]]); k2:=find(pos[y[i]]); o[x[i]]:=(o[x[i]]+re[pos[x[i]]])and 1; o[y[i]]:=(o[y[i]]+re[pos[y[i]]]) and 1; if (k1>0)or(k2>0) then if (k1>0)and(k2>0) then if k1=k2 then begin if o[x[i]]=1 then opp(k1); link(i,k1); if s[i]-2*f[i]<0 then opp(i); end else begin if o[x[i]]=1 then opp(k1); if o[y[i]]=0 then opp(k2); link(i,k1); link(i,k2); if s[i]-2*f[i]<0 then opp(i); end else if k1>0 then begin if o[x[i]]=1 then opp(k1); link(i,k1); if s[i]-2*f[i]<0 then opp(i); end else begin if o[y[i]]=0 then opp(k2); link(i,k2); if s[i]-2*f[i]<0 then opp(i); end; pos[x[i]]:=i; pos[y[i]]:=i; o[x[i]]:=1; o[y[i]]:=0; end; writeln(ans); end.