#include #include #include #include #define rep(i,n) for(int i = 0; i < n; i++) //[0,n) using namespace std; const int iinf = (int)1e9+1; struct Edge { int to; long long cost; int from; }; bool compe(const Edge &e,const Edge &e2){ return e.cost < e2.cost; } template int siz(T& a){return (int)a.size();} int main(){ int N,Q; cin >> N >> Q; vector A(N),B(Q); rep(i,N) cin >> A[i]; rep(i,Q) cin >> B[i]; vector>> G(N*Q+1); rep(j,Q){ rep(i,N){ if(i == N-1) continue; G[j*N+i].emplace_back(j*N+i+1,1); G[j*N+i+1].emplace_back(j*N+i,1); } rep(i,N){ if(A[i] != B[j]) continue; if(j != Q-1) G[j*N+i].emplace_back((j+1)*N+i,0); else G[j*N+i].emplace_back(N*Q,0); } } vector dist(N*Q+1,iinf); deque D; D.push_back(0); dist[0] = 0; while(siz(D)){ int v = D.front(); D.pop_front(); for(auto [nv,c] : G[v]){ int d = dist[v]+c; if(d < dist[nv]){ dist[nv] = d; if(c) D.push_back(nv); else D.push_front(nv); } } } cout << dist[N*Q]; }