#include <iostream>
#include<string>
#include<map>
using namespace std;
// 扫描服务器IP时,依次对比是否存在相同的代理IP
// 当扫描到相同的时,标记该IP为true
// 代表 从头扫描到该服务器IP时,使用其他为false的ip,可以不用切换,
// 若全部都被标记,代表从头到现在,所有IP都会出现,必须要切换一次了
// 最后一个被标记的代理IP 就作为前面所使用的代理IP,切换IP时,其他代理IP变回false
// 以当前服务器IP为起点,再次访问
int main() {
int n, m;
cin >> n;
map<string, bool> proxyIP;
string temp;
for (int i = 0; i < n; i++) {
cin >> temp;
proxyIP[temp] = false; // 记录各个代理IP是否被发现
}
cin >> m;
string IP;
int sum = 0;
for (int i = 0; i < m; i++) {
cin >> IP;
if (proxyIP.count(IP)) { // 存在相同的代理IP
if (proxyIP.size() == 1) { // 只有一个代理IP时,没有其他方案
sum = -1;
break;
}
proxyIP[IP] = true; // 标记已存在
// 是否都已经出现
bool all = true;
for (auto it = proxyIP.begin(); it != proxyIP.end();it++) { // 利用迭代器进行遍历
if ((*it).second == false) {
all = false;
break;
}
}
if (all) {// 全都出现
sum++;
// 全都初始化
for (auto it = proxyIP.begin(); it != proxyIP.end();it++) { // 利用迭代器进行遍历
(*it).second=false;
}
proxyIP[IP]=true;// 当前代理IP记录为出现
}
}
}
cout<<sum<<endl;
}
// 64 位输出请用 printf("%lld")