#include <bits/stdc++.h>

using ll = long long;
using namespace std;

ll dp[2010][2010];

int countnum(int x,int* A, int begin, int end){
    int cnt = 0;
    for (int i = begin; i < end; i++){
        if (A[i] == x)  cnt++;
    }
    return cnt;
}

int min(){
    int N, M;
    cin >> N >> M;
    int S[N];
    int T[M];

    for (int i = 0; i < N; i++) cin >> S[i];
    for (int i = 0; i < M; i++) cin >> T[i];

    int i = 0;
    int j = 0;
    ll res = 1;
    int ss, tt;
    int MAX = max(N,M);
    while ( i < MAX || j < MAX){
        if ( i == j ){
            if (S[i] == T[j]){
                res += i+1;
            } else {
                 ss = countnum(S[i], S, 0, i);
                 tt = countnum(S[i], T, 0, i);
                res += ss + min(ss,tt)+1;
                 ss = countnum(T[i], S, 0, i);
                 tt = countnum(T[i], T, 0, i);
                res +=  + min(ss,tt)+1;
            }
        }
        else {
            if ( i > j ){
                 ss = countnum(S[i], S, 0, i);
                 tt = countnum(S[i], T, 0, j);
                res += ss + min(ss,tt)+1;
            }else{
                 ss = countnum(T[j], S, 0, i);
                 tt = countnum(T[j], T, 0, j);
                res +=  + min(ss,tt)+1;
            }

        }
        if ( i < N) i++;
        if ( j < M) j++;
    }


        cout <<  res << endl;
}







    

    
