#include <bits/stdc++.h>
const int N = 300000;
int a[N + 10];
int u[N + 10];
int v[N + 10];
std::vector<int> prs[N + 10];
int show[N + 10];
int main() {
  int n = 0;
  int m = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", u + i, v + i);
    prs[u[i]].push_back(i);
    prs[v[i]].push_back(i);
  }
  long long res = 0;
  int r = 0;
  int num_2 = 0;
  for (int i = 1; i <= n; i++) {
    while (r <= n && !num_2) {
      r++;
      for (int j = 0; j < prs[a[r]].size(); j++) {
        show[prs[a[r]][j]]++;
        if (show[prs[a[r]][j]] > 1) num_2++;
      }
    }
    res += (r - i);
    for (int j = 0; j < prs[a[i]].size(); j++) {
      if (show[prs[a[i]][j]] > 1) num_2--;
      show[prs[a[i]][j]]--;
    }
  }
  printf("%I64d\n", res);
  return 0;
}
