#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, m;
int pe[300002], nr, cr[300002];
bool viz[300002], viz2[300002];
vector<int> v[300002], tr[300002], ctc[300002];
stack<int> s;
int gi;
int gr[300002], pz[300002];
bool isg[300002];
deque<int> d;
int mx[300002], mn[300002];
void dfs(int nod) {
  viz[nod] = 1;
  for (int j = 0; j < v[nod].size(); ++j) {
    int vecin = v[nod][j];
    if (!viz[vecin]) dfs(vecin);
  }
  s.push(nod);
}
void dfs2(int nod) {
  viz2[nod] = 1;
  ctc[nr].push_back(nod);
  cr[nod] = nr;
  for (int j = 0; j < tr[nod].size(); ++j) {
    int vecin = tr[nod][j];
    if (!viz2[vecin]) dfs2(vecin);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> pe[i], pz[pe[i]] = i;
  gi = pe[n];
  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    tr[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i)
    if (!viz[i]) dfs(i);
  while (!s.empty()) {
    int nod = s.top();
    s.pop();
    if (!viz2[nod]) {
      ++nr;
      dfs2(nod);
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < v[i].size(); ++j) ++gr[cr[v[i][j]]];
  for (int i = 1; i <= nr; ++i) {
    if (!gr[i]) d.push_back(i);
    mx[i] = 0;
    mn[i] = n + 1;
    for (int j = 0; j < ctc[i].size(); ++j) {
      mx[i] = max(mx[i], pz[ctc[i][j]]);
      mn[i] = min(mn[i], pz[ctc[i][j]]);
    }
  }
  isg[cr[gi]] = 1;
  int aa = n;
  while (!d.empty()) {
    int nod = d[0];
    int ft = n + 1;
    for (int i = 0; i < ctc[nod].size(); ++i) {
      int aa = ctc[nod][i];
      for (int j = 0; j < tr[aa].size(); ++j) {
        int vc = tr[aa][j];
        ft = min(ft, mn[cr[vc]]);
      }
    }
    if (ft <= mx[nod])
      for (int i = 0; i < ctc[nod].size(); ++i) {
        int aa = ctc[nod][i];
        for (int j = 0; j < tr[aa].size(); ++j) {
          int vc = tr[aa][j];
          mn[nod] = min(mn[nod], mn[cr[vc]]);
          mx[nod] = max(mx[nod], mx[cr[vc]]);
        }
      }
    if (isg[nod]) aa = min(aa, mn[nod]);
    d.pop_front();
    for (int i = 0; i < ctc[nod].size(); ++i) {
      int aa = ctc[nod][i];
      for (int j = 0; j < v[aa].size(); ++j) {
        int ve = v[aa][j];
        --gr[cr[ve]];
        if (gr[cr[ve]] == 0) d.push_back(cr[ve]);
      }
    }
    cout << nod << " " << mn[nod] << " " << mx[nod] << '\n';
  }
  cout << n - aa << '\n';
  return 0;
}
