// WA/TLE/RE

#include <iostream>
#include <vector>

using namespace std;

bool ready(vector<int> &v, int n) {
    for (int i = 1; i <= n; i++) {
        if (v[i] != i) return false;
    }
    return true;
}

int calc(vector<int> v, int n, int a, int b, int f) {
    int c = 0;
    int r = 0;
    vector<int> z(n+1);
    int g = 0;
    while (true) {
        if (ready(v, n)) return c;
        auto u = v;
        r++;
        if (f == 1) {
            for (int i = 1; i <= a; i++) {
                z[v[i]] = r;
            }
            int j = 1;
            for (int i = 1; i <= n && j <= a; i++) {
                if (z[i] == r) {
                    v[j++] = i;
                }
            }
        }
        if (f == 2) {
            for (int i = n-b+1; i <= n; i++) {
                z[v[i]] = r;
            }
            int j = n-b+1;
            for (int i = 1; i <= n && j <= n; i++) {
                if (z[i] == r) {
                    v[j++] = i;
                }
            }
        }
        f = 3-f;
        c++;
        if (u == v) g++;
        else g = 0;
        if (g > 2) return 1e9;
    }
}

int main() {
    int n, q;
    cin >> n >> q;
    vector<int> v(n+1);
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (int i = 1; i <= q; i++) {
        int a, b;
        cin >> a >> b;
        int c1 = calc(v, n, a, b, 1);
        int c2 = calc(v, n, a, b, 2);
        int r = min(c1, c2);
        if (r == 1e9) r = -1;
        cout << r << "\n";
    }

}
