#include <bits/stdc++.h>
using i64 = int64_t;
using u64 = uint64_t;
using u32 = uint32_t;
int n, q;
constexpr int N = 5000;
int a[N], p[N], f[131072];
std::vector<std::pair<int, int>> ans;
struct SegmentTree {
  std::vector<int> pos[N * 4];
  std::map<std::pair<int, int>, int> map[N * 4];
  void build(int l, int r, int p) {
    if (l == r) {
      return void(pos[p].emplace_back(::p[l]));
    } else {
      int mid = l + r >> 1;
      int ls = p << 1;
      int rs = p << 1 | 1;
      build(l, mid, ls);
      build(mid + 1, r, rs);
      int p0(0), p1(0), p2(0);
      pos[p].resize(r - l + 1);
      while (p0 < pos[ls].size() && p1 < pos[rs].size()) {
        pos[p][p2++] =
            pos[ls][p0] < pos[rs][p1] ? pos[ls][p0++] : pos[rs][p1++];
      }
      while (p0 < pos[ls].size()) {
        pos[p][p2++] = pos[ls][p0++];
      }
      while (p1 < pos[rs].size()) {
        pos[p][p2++] = pos[rs][p1++];
      }
    }
  }
  int solve(int L, int R, int p) {
    int l = std::lower_bound(pos[p].begin(), pos[p].end(), L) - pos[p].begin();
    int r =
        std::upper_bound(pos[p].begin(), pos[p].end(), R) - pos[p].begin() - 1;
    if (l > r) {
      return 0;
    }
    if (l == r) {
      return pos[p][l];
    } else if (map[p].count({l, r})) {
      return map[p][{l, r}];
    } else {
      int x = solve(L, R, p << 1);
      int y = solve(L, R, p << 1 | 1);
      if (!x || !y) {
        return map[p][{l, r}] = x | y;
      } else {
        ans.emplace_back(x, y);
        return map[p][{l, r}] = (int)ans.size() + n;
      }
    }
  }
} T;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    std::cin >> a[i];
    p[a[i]] = i;
  }
  T.build(1, n, 1);
  for (int i = 1; i <= q; i++) {
    int l, r;
    std::cin >> l >> r;
    f[i] = T.solve(l, r, 1);
  }
  std::cout << (int)ans.size() + n << "\n";
  for (auto i : ans) {
    std::cout << i.first << " " << i.second << "\n";
  }
  for (int i = 1; i <= q; i++) {
    std::cout << f[i] << " ";
  }
  return 0;
}
