#include <bits/stdc++.h>
template <class T>
void rd(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int N = (1 << 12) + 10;
using namespace std;
int n, q, cnt, a[N], hs[N];
struct Node {
  vector<int> q;
  map<pair<int, int>, int> S;
} f[N << 2];
void build(int o, int l, int r) {
  vector<int> &t = f[o].q;
  if (l == r)
    t.push_back(hs[l]);
  else {
    build((o << 1), l, (l + r >> 1));
    build((o << 1 | 1), (l + r >> 1) + 1, r);
    vector<int> &t1 = f[(o << 1)].q;
    vector<int> &t2 = f[(o << 1 | 1)].q;
    t.resize(r - l + 1);
    merge(t1.begin(), t1.end(), t2.begin(), t2.end(), t.begin());
  }
}
vector<pair<int, int> > op;
vector<int> ans;
int work(int l, int r, int o) {
  vector<int> &t = f[o].q;
  int L = lower_bound(t.begin(), t.end(), l) - t.begin();
  int R = upper_bound(t.begin(), t.end(), r) - t.begin() - 1;
  if (L > R) return -1;
  if (L == R) return t[L];
  map<pair<int, int>, int> &u = f[o].S;
  if (u.find(make_pair(L, R)) == u.end()) {
    int a1 = work(l, r, (o << 1));
    int a2 = work(l, r, (o << 1 | 1));
    if (a1 != -1 && a2 != -1) {
      u[make_pair(L, R)] = ++cnt;
      op.push_back(make_pair(a1, a2));
    } else
      u[make_pair(L, R)] = max(a1, a2);
  }
  return u[make_pair(L, R)];
}
int main() {
  rd(n);
  rd(q);
  cnt = n;
  for (int i = 1; i <= n; i++) {
    rd(a[i]);
    hs[a[i]] = i;
  }
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l, r;
    rd(l);
    rd(r);
    ans.push_back(work(l, r, 1));
  }
  printf("%d\n", cnt);
  for (int i = 0; i < op.size(); i++)
    printf("%d %d\n", op[i].first, op[i].second);
  for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
  puts("");
  return 0;
}
