#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
vector<int> a(n), b(m);
vector<int> pre(m),suf(m);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<m;i++){
cin>>b[i];
}
int j=0;
for(int i=0;i<n;i++){
if(a[i]==b[j]){
pre[j]=i;
j++;
}
if(j==m){
break;
}
}
j=m-1;
for(int i=n-1;i>=0;i--){
if(a[i]==b[j]){
suf[j]=i;
j--;
}
if(j==-1){
break;
}
}
int ans=0;
for (int i = 0; i < m; i++) {
int l = (i > 0) ? pre[i - 1] : -1; // Handling edge case
int r = (i < m - 1) ? suf[i + 1] : n; // Handling edge case
if (l < r) {
ans++;
}
}
cout<<ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkgewoJaW50IG4sbTsKCWNpbj4+bj4+bTsKCXZlY3RvcjxpbnQ+IGEobiksIGIobSk7Cgl2ZWN0b3I8aW50PiBwcmUobSksc3VmKG0pOwoJZm9yKGludCBpPTA7aTxuO2krKyl7CgkJY2luPj5hW2ldOwoJfQoJZm9yKGludCBpPTA7aTxtO2krKyl7CgkJY2luPj5iW2ldOwoJfQoJaW50IGo9MDsKCWZvcihpbnQgaT0wO2k8bjtpKyspewoJCWlmKGFbaV09PWJbal0pewoJCQlwcmVbal09aTsKCQkJaisrOwoJCX0KCQlpZihqPT1tKXsKCQkJYnJlYWs7CgkJfQoJfQoJaj1tLTE7Cglmb3IoaW50IGk9bi0xO2k+PTA7aS0tKXsKCQlpZihhW2ldPT1iW2pdKXsKCQkJc3VmW2pdPWk7CgkJCWotLTsKCQl9CgkJaWYoaj09LTEpewoJCQlicmVhazsKCQl9Cgl9CglpbnQgYW5zPTA7Cglmb3IgKGludCBpID0gMDsgaSA8IG07IGkrKykgewogICAgICAgIGludCBsID0gKGkgPiAwKSA/IHByZVtpIC0gMV0gOiAtMTsgIC8vIEhhbmRsaW5nIGVkZ2UgY2FzZQogICAgICAgIGludCByID0gKGkgPCBtIC0gMSkgPyBzdWZbaSArIDFdIDogbjsgLy8gSGFuZGxpbmcgZWRnZSBjYXNlCiAgICAgICAgaWYgKGwgPCByKSB7CiAgICAgICAgICAgIGFucysrOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQ8PGFuczsKICAgIHJldHVybiAwOwp9