#include <iostream>
#include <algorithm>
#include <vector>
//#include <bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;

void solve() {
    int n, q;
    cin >> n >> q;
    vi au(n);
    for (int i = 0; i < n; ++i) {
        cin >> au[i];
    }
    vi res(q);
    int itres = 0;
    while (q--) {
        int l, r;
        cin >> l >> r;
        int velna = r - l + 1;
        vl a(velna);
        int it = 0;
        for (int i = l - 1; i < r; i++) {
            a[it] = au[i];
            it++;
        }
        it--;
        sort(a.begin(), a.end());
        ll k = 0;

        bool voj = false;
        bool prosli = false;
        for (int i = 0; i < it; ++i) {
            if (a[i] > 1 || a[i+1] > 1) voj = true;
            if (a[i] == 1) { continue; }
            if(a[i]/4==0) {continue;}
            else if (a[i] == a[i + 1] + 1 || a[i] == a[i + 1] - 1) {
                k+=2;
            }
            else if (a[i] == a[i + 1] && a[i] != 1) { k++; }
        }
        if (k == 0 && voj) k++;
        res[itres] = k;
        itres++;
    }
    for (int i = 0; i < itres; ++i) { cout << res[i] << endl; }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();
    return 0;
}