#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define fuck(x) cout << (x) << endl
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int N = 2e5 + 5;
int n, q;
char s[N], t[N];
struct mat{
int a[5][5];
mat() { memset(a, INF, sizeof(a)); }
mat operator * (const mat& b) const{
mat c;
for(int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
for(int k = 0; k < 5; k++)
c.a[i][j] = min(c.a[i][j], a[i][k] + b.a[k][j]);
return c;
}
} tr[N<<2];
void build(int l, int r, int rt){
if(l == r){
for(int i = 0; i < 5; i++)
tr[rt].a[i][i] = 0;
if(s[l] == '2') tr[rt].a[0][0] = 1, tr[rt].a[0][1] = 0;
if(s[l] == '0') tr[rt].a[1][1] = 1, tr[rt].a[1][2] = 0;
if(s[l] == '1') tr[rt].a[2][2] = 1, tr[rt].a[2][3] = 0;
if(s[l] == '9') tr[rt].a[3][3] = 1, tr[rt].a[3][4] = 0;
if(s[l] == '8') tr[rt].a[3][3] = 1, tr[rt].a[4][4] = 1;
return;
}
int m = l + r >> 1;
build(lson);
build(rson);
tr[rt] = tr[rt << 1] * tr[rt << 1 | 1]; // pushup
}
mat query(int l, int r, int rt, int L, int R){
if(L <= l && r <= R)
return tr[rt];
int m = l + r >> 1;
if(R <= m) // 全在左边
return query(lson, L, R);
if(m < L) // 全在右边
return query(rson, L, R);
return query(lson, L, R) * query(rson, L, R);
}
int main(){
scanf("%d%d %s", &n, &q, t+1);
for(int i = 1; i <= n; i++){
s[i] = t[n - i + 1];
}
build(1, n, 1);
for (int i = 1, x, y; i <= q; i++){
scanf("%d%d", &x, &y);
mat ans = query(1, n, 1, n-y+1, n-x+1);
int res = ans.a[0][4] > n ? -1 : ans.a[0][4];
printf("%d\n", res);
}
return 0;
}