#include <functional> #include <iostream> #include <cstring> #include <string> #include <vector> #include <algorithm> using namespace std; int myfind(string x,vector<string> t,int begin){ int loc=t.size(); for(int i=begin;i<t.size();i++){ if(x==t[i]) { loc=i; return loc; } } return loc; } bool alleaqual(string x,vector<string> t){ int i=0; bool r=true; while(i<t.size()) { if(x!=t[i]) { r=false; break; } i++; } return r; } int main() { int n,m; while(cin>>n){ int a,b; a=n; vector<string> agency; vector<string> server; while(a>0){ string i; cin>>i; agency.push_back(i); a--; } cin>>m; b=m; int f=1; while(b>0){ string j; cin>>j; server.push_back(j); b--; } if(alleaqual(agency[0], server)){ cout<<-1<<endl; continue; } int apt[n]; int t=0; int times=0; while(t<server.size()){ for(int i=0;i<n;i++){ apt[i]=myfind(agency[i], server,t); } sort(apt,apt+n,greater<int>()); if(t==0 && apt[0]==server.size()) { f=0; break; } t=apt[0]; times++; } times--; if(times==m-1) cout<<-1<<endl; else cout<<times<<endl; } }