50 50
75 71 23 37 28 23 69 75 5 62 3 11 96 100 13 50 57 51 8 90 4 6 84 27 11 89 95 81 10 62 48 52 69 87 97 95 30 74 21 42 36 64 31 80 81 50 56 53 33 99
26 30 5 33 35 29 6 15 36 17 32 16 14 1 29 34 22 40 12 42 38 48 39 50 13 47 18 43 10 8 49 45 11 31 21 37 46 28 20 41 2 7 9 24 27 23 3 44 15 14
 10 10
61 59 97 16 2 94 57 48 91 93
2 8 6 5 3 1 3 4 9 10
 2 1
1 2
1
 3 3
10 20 30
1 2 3
 50 60
86 57 45 93 17 12 40 10 47 80 18 80 3 9 6 55 13 99 5 76 4 70 100 55 27 91 71 3 65 93 41 74 80 56 90 50 58 13 71 9 47 52 26 73 72 21 15 81 88 28
40 32 5 16 49 23 3 17 14 10 1 15 1 21 28 22 13 45 12 25 44 48 46 32 36 43 11 8 49 7 7 35 10 14 39 4 42 10 30 27 1 17 31 15 8 41 44 33 25 26 19 18 29 37 50 6 36 38 47 9
 2 7
20 30
1 1 1 2 2 2 2
 2 2
10 12
2 1
 5 1
16 87 36 16 81
3
 2 3
20 30
1 1 1
 2 10
39 26
1 1 2 2 2 2 2 2 1 2

63929
 2137
 0
 40
 62514
 20
 12
 0
 0
 104

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define int long long
#define sr string
#define x first
#define y second
#define yy cout<<"YES\n"
#define nn cout<<"NO\n"
#define syy cout<<"Yes\n"
#define snn cout<<"No\n"
#define rt return
#define ct continue
#define br break
#define pb push_back
#define mt make_tuple
#define ef else if
#define ls else
#define yo while
#define vt vector
#define qu queue
#define sk stack
#define prq priority_queue
#define pr pair
#define gt greater
#define nxtp next_permutation
#define all(c) (c).begin(),(c).end()
#define all1(c) (c).begin()+1,(c).end()
#define bg begin
#define bs binary_search
#define upb upper_bound
#define lwb lower_bound
#define rsh random_shuffle
#define ins insert
#define ook order_of_key
#define fbo find_by_order
#define oset tree <int,null_type,less <int>,rb_tree_tag,tree_order_statistics_node_update>
#define mmod 1000000007
#define pmod 998244353
#define mii map <int,int>
#define mib map <int,bool>
#define pii pair <int,int>
#define tii tuple <int,int,int>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
const int d4x[8]={-1,0,1,0},d4y[4]={0,1,0,-1};
const int d8x[8]={-1,-1,0,1,1,1,0,-1},d8y[8]={0,1,1,1,0,-1,-1,-1};
int gcd(int a,int b){
    yo (b) b^=a^=b^=a%=b;
    rt a;
}
int lcm(int a,int b){
    rt a*b/gcd(a,b);
}
int bigmod(int b,int p,int m){
    int ans=1;
    b%=m;
    if (!b) return 0;
    yo (p){
        if (p&1) ans=(ans*b)%m;
        p>>=1;
        b=(b*b)%m;
    }
    rt ans;
}
int modinv(int a,int m){
	rt bigmod(a,m-2,m);
}
int ncr(int n,int r){
	if (n<r) rt 0ll;
	int ans=1,k=min(n-r,r);
	for (int i=1; i<=k; i++) ans=ans*(n-i+1)/i;
	rt ans;
}
double eu(int x1,int y1,int x2,int y2){
	int dx=abs(x1-x2),dy=abs(y1-y2);
	rt sqrt(dx*dx+dy*dy);
}
int mht(int x1,int y1,int x2,int y2){
	int dx=abs(x1-x2),dy=abs(y1-y2);
	rt dx+dy;
}
int ham(string s,string t){
	int ans=0;
	for (int i=0; i<s.size(); i++) ans+=(s[i]!=t[i]);
	rt ans;
}
int ce(int a,int b){
	if (a%b==0) rt a/b;
	ls rt a/b+1;
}
struct dsu{
	int pa[000];
	void init(int n){
		for (int i=0; i<=n; i++) pa[i]=i;
	}
	int prt(int n){
		if (pa[n]==n) rt n;
		rt pa[n]=prt(pa[n]);
	}
	void unn(int u,int v){
		if (prt(u)==prt(v)) rt;
		pa[prt(u)]=pa[prt(v)];
	}
}dsu;
struct bit{
	int tree[000];
	void add(int pos,int val){
		for (int i=pos; i<000; i+=i&-i) tree[i]+=val;
	}
	int query(int pos){
		int ans=0;
		for (int i=pos; i; i-=i&-i) ans+=tree[i];
		rt ans;
	}
}bit;
int segta[000];
struct segtr{
	int seg[000];
	void build(int v,int tl,int tr){
    	if (tl==tr){
        	seg[v]=segta[tl];
        	rt;
    	}
    	int tm=(tl+tr)>>1;
    	build(v+v,tl,tm);
    	build(v+v+1,tm+1,tr);
    	int ff=seg[v+v],ss=seg[v+v+1];
    	seg[v]=max(ff,ss);
	}
	void update(int v,int tl,int tr,int pos,int val){
    	if (tl==tr){
        	seg[v]=val;
        	return;
	    }
    	int tm=(tl+tr)>>1;
    	if (pos<=tm) update(v+v,tl,tm,pos,val);
    	ls update(v+v+1,tm+1,tr,pos,val);
    	int ff=seg[v+v],ss=seg[v+v+1];
    	seg[v]=max(ff,ss);
	}
	int query(int v,int tl,int tr,int l,int r){
    	if (l>r) rt 0;
    	if (tl==l&&tr==r) rt seg[v];
    	int tm=(tl+tr)>>1;
    	if (r<=tm) rt query(v+v,tl,tm,l,r);
    	if (l>tm) rt query(v+v+1,tm+1,tr,l,r);
    	int ff=query(v+v,tl,tm,l,tm),ss=query(v+v+1,tm+1,tr,tm+1,r);
    	rt max(ff,ss);
	}
}segtr;
void solve(int tcno){
	int n,m;
	cin>>n>>m;
	int w[n],b[m];
	for (int i=0; i<n; i++) cin>>w[i];
	for (int i=0; i<m; i++) cin>>b[i];
	pii a[n+1];
	for (int i=1; i<=n; i++){
		a[i].x=1e9;
		a[i].y=i;
	}
	for (int i=0; i<m; i++) a[b[i]].x=min(a[b[i]].x,i);
	sort(a+1,a+n+1);
	vt <int> v;
	for (int i=1; i<=n; i++) v.pb(a[i].y);
	int ans=0;
	for (int i=0; i<m; i++){
		int wh;
		for (int j=0; j<n; j++){
			if (v[j]==b[i]){
				wh=j;
				br;
			}
		}
		ans+=accumulate(v.begin(),v.begin()+wh,0ll);
		v.erase(v.begin()+wh);
		v.insert(v.begin(),b[i]);
		//cout<<ans<<endl;
	}
	cout<<ans<<endl;
}
signed main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	if (1){
		ios::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
	}
    int t=1;
    //cin>>t;
    for (int i=1; i<=t; i++) solve(i);
}