#include <bits/stdc++.h>
using namespace std;
struct seg {
  vector<int> id;
  map<pair<int, int>, int> st;
  int getid(int l, int r) {
    int L = lower_bound(id.begin(), id.end(), l) - id.begin();
    int R = upper_bound(id.begin(), id.end(), r) - id.begin() - 1;
    if (L > R) return -1;
    return st[make_pair(L, R)];
  }
} tr[20000];
vector<pair<int, int> > res;
int cur;
int merge(int x, int y) {
  if (x == -1 || y == -1)
    return max(x, y);
  else {
    res.emplace_back(x, y);
    cur++;
    return cur;
  }
}
int blk, tot, Tid = 0;
int n, q, a[5000], Id[5000], l[5000], r[5000];
int solve(int l, int r, vector<int> id) {
  Tid++;
  int k;
  tr[k = Tid].id = id;
  if (l == r) {
    tr[Tid].st[pair<int, int>(0, 0)] = id[0];
    return Tid;
  }
  int mid = (l + r) >> 1;
  vector<int> lid, rid;
  for (auto x : id)
    if (a[x] <= mid)
      lid.push_back(x);
    else
      rid.push_back(x);
  int ls = solve(l, mid, lid), rs = solve(mid + 1, r, rid);
  for (int i = 0; i < id.size(); i++)
    for (int j = i; j < id.size(); j++)
      tr[k].st[pair<int, int>(i, j)] =
          merge(tr[ls].getid(id[i], id[j]), tr[rs].getid(id[i], id[j]));
  return k;
}
int main() {
  scanf("%d%d", &n, &q);
  blk = sqrt(q);
  cur = n;
  for (int i = 1; i <= (tot = n / blk); i++)
    l[i] = r[i - 1] + 1, r[i] = i * blk;
  if (tot * blk != n) l[++tot] = r[tot - 1] + 1, r[tot] = n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= tot; i++) {
    vector<int> id;
    for (int j = 1; j <= n; j++)
      if (l[i] <= a[j] && a[j] <= r[i]) id.push_back(j);
    Id[i] = solve(l[i], r[i], id);
  }
  vector<int> ans;
  for (int l, r; q--;) {
    scanf("%d%d", &l, &r);
    int rest = -1;
    for (int i = 1; i <= tot; i++) rest = merge(rest, tr[Id[i]].getid(l, r));
    ans.push_back(rest);
  }
  printf("%d\n", cur);
  for (auto x : res) printf("%d %d\n", x.first, x.second);
  for (int x : ans) printf("%d ", x);
}
