#include <bits/stdc++.h>   using namespace std;   vector<string> split(const string& s, char c) {     vector<string> v;     stringstream ss(s);     string first;     while (getline(ss, first, c)) v.emplace_back(first);     return move(v);   }   void err(vector<string>::iterator it) {}   template <typename T, typename... Args>   void err(vector<string>::iterator it, T a, Args... args) {     cerr << a <<    ;     err(++it, args...);   }   const int M = 5000100, mod = 1000000007;   struct segment_tree {     pair<int, int> T[M];     segment_tree() {}     pair<int, int> find_min(pair<int, int> a, pair<int, int> b) {       return a.first < b.first ? a : b;     }     void build(int* A, int i, int l, int r) {       if (l == r)         T[i] = make_pair(A[l], l);       else {         int m = (l + r) / 2;         build(A, 2 * i, l, m);         build(A, 2 * i + 1, m + 1, r);         T[i] = find_min(T[2 * i], T[2 * i + 1]);       }     }     void update(int i, int l, int r, int first) {       if (l == r && r == first)         T[i] = make_pair(1111111111, l);       else if (first >= l && first <= r) {         int m = (l + r) / 2;         update(2 * i, l, m, first);         update(2 * i + 1, m + 1, r, first);         T[i] = find_min(T[2 * i], T[2 * i + 1]);       }     }     pair<int, int> query(int i, int l, int r, int st, int ed) {       if (l >= st && r <= ed)         return T[i];       else if (l > ed || r < st)         return make_pair(1111111111, -1);       else {         int m = (l + r) / 2;         return find_min(query(2 * i, l, m, st, ed),                         query(2 * i + 1, m + 1, r, st, ed));       }     }   };   struct BIT {     vector<int> tree;     BIT(int n) { tree.resize(n); }     void update(int idx, int val) {       while (idx < tree.size()) {         tree[idx] += val;         idx += idx & (-idx);       }     }     int query(int idx) {       int sum = 0;       while (idx > 0) {         sum += tree[idx];         idx -= idx & (-idx);       }       return sum;     }   };   int A[M], spec[M], n;   segment_tree tree;   BIT bit(M);   long long solve(int l, int r) {     long long sum = 0;     while (l <= r) {       pair<int, int> temp = tree.query(1, 1, n, l, r);       if (temp.first == 1111111111)         break;       else if (spec[temp.first]) {         tree.update(1, 1, n, temp.second);         bit.update(temp.second, -1);         sum += solve(l, temp.second - 1);         sum += solve(temp.second + 1, r);         break;       } else {         sum += bit.query(r) - bit.query(l - 1);         tree.update(1, 1, n, temp.second);         bit.update(temp.second, -1);       }     }     return sum;   }   int main() {     ios_base::sync_with_stdio(false);     int i, j, k, m;     memset((spec), (false), sizeof(spec));     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++) scanf( %d , &A[i]);     for (j = 1; j <= m; j++) scanf( %d , &k), spec[k] = true;     tree.build(A, 1, 1, n);     for (i = 1; i <= n; i++) bit.update(i, 1);     cout << solve(1, n) << endl;     return 0;   }   