#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 100000;
vector<int> v1;
vector<int> v2;
vector<int> otr;
int n;
int res = 1;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        v1.push_back(a);
        if(i == 0 || i == n - 1)
        {
            otr.push_back(1);
        }
        else
        {
            otr.push_back(0);
        }

    }
    for(int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        v2.push_back(a);
    }
    while(!v1.empty())
    {
        int start1, end1, start2, end2, currLen = 0, longest = 0;
        vector<int> same;
        for(int i = 0; i < n; i++)
        {
            for(int v = 0; v < n; v++)
            {
                if(v1[i] == v2[v])
                {
                    same.push_back(v);
                }
            }

            for(int nach = 0; nach < same.size(); nach++)
            {
                int j = same[nach];
                currLen = 0;
                for(int len = 0; len <= v1.size() - 1; len++)
                {
                    if(v1[i + len - v1.size() * (len + i >= v1.size() ? 1 : 0)] != v2[j + len - v2.size() * (j + len >= v2.size() ? 1 : 0)])
                    {
                        if(currLen > longest)
                        {
                            longest = currLen;
                            start1 = i;
                            end1 = i + currLen - 1;
                            start2 = j;
                            end2 = j + currLen - 1;
                            start1 -= v1.size() * (start1 >= v1.size() ? 1 : 0);
                            end1 -= v1.size() * (end1 >= v1.size() ? 1 : 0);
                            start2 -= v2.size() * (start2 >= v2.size() ? 1 : 0);
                            end2 -= v2.size() * (end2 >= v2.size() ? 1 : 0);

                        }
                        break;
                    }
                    else
                    {
                        currLen++;
                        if(len == v1.size() - 1)
                        {
                            if(currLen > longest)
                            {
                                longest = currLen;
                                start1 = i;
                                end1 = i + currLen - 1;
                                start2 = j;
                                end2 = j + currLen - 1;
                                start1 -= v1.size() * (start1 >= v1.size() ? 1 : 0);
                                end1 -= v1.size() * (end1 >= v1.size() ? 1 : 0);
                                start2 -= v2.size() * (start2 >= v2.size() ? 1 : 0);
                                end2 -= v2.size() * (end2 >= v2.size() ? 1 : 0);
                            }
                            break;
                        }
                    }
                }
            }
        }
        //cout << start1 << "; " << end1 << endl;
        //cout << start2 << "; " << end2 << endl;
        if(start1 == start2 && end1 == end2 && longest == n)
        {
            cout << 1;
            return 0;
        }
        if(start1 == start2 && end1 == end2 && longest == v1.size())
        {
            cout << res;
            return 0;
        }

        if(start2 <= end2)
        {
            if(start1 != end1)
            {
                res += 2 - otr[start1] - otr[end1];
            }
            else
            {
                res += 2 - max(otr[start1], otr[end1]);
            }
        }
        else
        {

            if(start1 == start2 && end1 == end2)
            {
                res += 2 - otr[start1] - otr[end1];
            }
            else
            {
                //cout << "eho: " << otr[start1] << ", " << otr[end1] << endl;
                res += 3 - otr[start1] - otr[end1];
                if(start2 == end2 + 1 && (otr[start1] == 2 || otr[end1] == 2))
                {
                    if(otr[start1] == 2 && otr[end1] == 2)
                    {
                        res++;
                    }

                }
            }

        }
        //cout << "res: " << res << endl;
        if(start1 <= end1)
        {
            v1.erase(v1.begin() + start1, v1.begin() + end1 + 1);
            if(start1 - 1 > 0)
            {
                otr[start1 - 1]++;
                if(otr[start1 - 1] > 2)
                {
                    otr[start1 - 1] = 2;
                }
            }
            if(end1 + 1 < n - 1)
            {
                otr[end1 + 1]++;
                if(otr[end1 + 1] > 2)
                {
                    otr[end1 + 1] = 2;
                }
            }

            otr.erase(otr.begin() + start1, otr.begin() + end1 + 1);
        }
        else
        {
            v1.erase(v1.begin() + start1, v1.end());
            v1.erase(v1.begin(), v1.begin() + end1 + 1);
            otr[end1 + 1]++;
            otr[start1 - 1]++;
            if(otr[end1 + 1] > 2)
            {
                otr[end1 + 1] = 2;
            }
            if(otr[start1 - 1] > 2)
            {
                otr[start1 - 1] = 2;
            }
            otr.erase(otr.begin() + start1, otr.end());
            otr.erase(otr.begin(), otr.begin() + end1 + 1);
        }
        if(start2 <= end2)
        {
            v2.erase(v2.begin() + start2, v2.begin() + end2 + 1);
        }
        else
        {
            v2.erase(v2.begin() + start2, v2.end());
            v2.erase(v2.begin(), v2.begin() + end2 + 1);
        }
    }
    cout << res;
    return 0;
}
/*
7
4 5 7 6 1 2 3
2 3 4 5 6 7 1*/
