#include <bits/stdc++.h>
using namespace std;
#define SZ(A) int(A.size())
#define fr first
#define sc second
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
const int N = int(1e6) + 5;
int A3[N], A2[N], Vis[N];
int main() {
int n, q, A; scanf("%d %d", &n, &q);
for(int i = 0; i < n; i++) {
scanf("%d", &A);
if(!i) {
Vis[A]++, A2[i] = (A%3 == 2),
A3[i] = (A == 3);
}else {
if(!Vis[A])
Vis[A]++, A2[i] = A2[i - 1] + (A%3 == 2),
A3[i] = A3[i - 1] + (A == 3);
else A2[i] = A2[i - 1] - (A%3 == 2),
A3[i] = A3[i - 1] - (A == 3);
}
}
while(q--) {
int l, r; scanf("%d %d", &l, &r);
--l, --r;
if(l) {
if(A3[r] >= A3[l - 1] && A3[r] - A3[l - 1]) puts("0");
else if(A2[r] >= A2[l - 1] && ((A2[r] - A2[l - 1]) & 1)) puts("2");
if(A3[r] < A3[l - 1] && A3[r] + A3[l - 1]) puts("0");
else if(A2[r] < A2[l - 1] && (A2[r] + A2[l - 1]) & 1) puts("2");
else puts("1");
}else {
if(A3[r]) puts("0");
else if(A2[r] & 1) puts("2");
else puts("1");
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBTWihBKSBpbnQoQS5zaXplKCkpIAojZGVmaW5lIGZyIGZpcnN0IAojZGVmaW5lIHNjIHNlY29uZCAKCnR5cGVkZWYgbG9uZyBsb25nIGxsOyAKdHlwZWRlZiBwYWlyPGludCwgaW50PiBpaTsgCnR5cGVkZWYgdmVjdG9yPGludD4gdmk7IAogCmNvbnN0IGludCBOID0gaW50KDFlNikgKyA1OyAKaW50IEEzW05dLCBBMltOXSwgVmlzW05dOyAKaW50IG1haW4oKSB7CglpbnQgbiwgcSwgQTsgc2NhbmYoIiVkICVkIiwgJm4sICZxKTsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKCQlzY2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