#define _CRT_SECURE_NO_WARNINGS #define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING #include using namespace std; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector p(n); for (auto &x:p) cin >> x; vector> canswap(m); for (auto &x:canswap) cin >> x.first >> x.second; set> S(canswap.begin(), canswap.end()); int targetval = *p.rbegin(); for (int i = n - 1; i; i--) if (S.count(make_pair(p[i - 1], p[i]))) { swap(p[i - 1], p[i]); for (int j = i + 1; j < n; j++) if (S.count(make_pair(p[j - 1], p[j]))) swap(p[j - 1], p[j]); else break; } cout << distance(p.rbegin(), find(p.rbegin(), p.rend(), targetval)); return 0; }