#include <iostream>
#include <vector>
using namespace std;
// 主要思路就是从开始找到最远的那个需要切换的位置,然后将当前的网址切换成最远的那个,这样只需要在最远的时候切换即可。
int main() {
   int n; cin >> n;
   vector<string> proxy(n);
   for(int i=0; i<n; i++) cin >> proxy[i];
   int m; cin >> m;
   vector<string> dest(m);
   for(int i=0; i<m; i++) cin >> dest[i];
   int movIndex = 0;
   int count = 0;
   int flag = 0;
   while(movIndex<m){
    //找到最远的color
    int maxIndex = movIndex;
    string current = dest[movIndex];
    int k = 0; // 用来记录在寻找下个需要切换的网址中,是否存在这样一个情况:
	 //proxy中有一个或者多个网址,在dest的movIndex位置之后中没有出现,如果是,那么就直接break。
    for(int colorIndex = 0; colorIndex<n; colorIndex++){
        if(proxy[colorIndex]==current) {
            flag =1;
            continue;
        }
        for(int j=movIndex; j<m; j++){
            if(dest[j]==proxy[colorIndex]){
                k++;
                maxIndex = max(maxIndex, j);
                break;
            }
        }
    }
    if(maxIndex > movIndex && k>=n-1){ 
            movIndex = maxIndex;
            count++;
    }else break;
   }

    if(flag==1 && proxy.size()==1)
        cout << -1 << endl;
    else
        cout << count << endl;
}