#include <bits/stdc++.h>
using namespace std;
int a[1234567];
std::vector<int> v[2000002];
std::map<int, int> m1;
std::map<pair<int, int>, int> m2;
int main() {
  int n, m, i;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = (int)0; i < (int)n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = (int)1; i < (int)m + 1; i++) {
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    if (a > b) swap(a, b);
    m2[make_pair(a, b)] = 1;
  }
  std::map<pair<int, int>, int>::iterator it;
  for (it = (m2).begin(); it != (m2).end(); it++) {
    int u = it->first.first;
    int vi = it->first.second;
    v[u].push_back(vi);
    v[vi].push_back(u);
  }
  long long curr = 1, back = 0;
  m1[a[0]] = 0;
  long long ans = 0;
  long long last = -1;
  while (curr < n) {
    int pos;
    while (curr < n) {
      pos = -1;
      for (int i = 0; i < v[a[curr]].size(); ++i) {
        int nx = v[a[curr]][i];
        if (m1.find(nx) != m1.end()) {
          pos = max(pos, m1[nx]);
        }
      }
      if (pos != -1) {
        break;
      } else {
        m1[a[curr]] = curr;
      }
      curr++;
    }
    if (last >= back) {
      long long len = last - back + 1;
      ans -= (len * (len + 1)) / 2;
    }
    ans = ans + (curr - back) * (curr - back + 1) / 2;
    while (back <= pos) {
      m1.erase(a[back]);
      back++;
    }
    last = curr - 1;
  }
  cout << ans << endl;
  return 0;
}
