#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int32_t &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(unsigned long long &x) { scanf("%llu", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int32_t &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.F);
  putchar(' ');
  _W(x.S);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int INF = INT_MAX / 2;
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
const int maxn = 2e5 + 10;
const int mo_num = 1e9 + 7;
int n, m, t, a[maxn], b[maxn], ans;
struct node {
  int f[5][5];
  node() {
    for (int i = 0; i <= 4; i++)
      for (int j = 0; j <= 4; j++) f[i][j] = i == j ? 0 : INF;
  }
} st[maxn << 2];
string k;
node merge(node z, node y) {
  node gget;
  for (int i = 0; i <= 4; i++) {
    for (int j = 0; j <= 4; j++) {
      gget.f[i][j] = INF;
      for (int l = 0; l <= 4; l++) {
        gget.f[i][j] = min(gget.f[i][j], z.f[i][l] + y.f[l][j]);
      }
    }
  }
  return gget;
}
void build(int rt, int l, int r) {
  if (l == r) {
    char g = k[l - 1];
    node &tmp = st[rt];
    if (g == '2')
      tmp.f[0][1] = 0, tmp.f[0][0] = 1;
    else if (g == '0')
      tmp.f[1][2] = 0, tmp.f[1][1] = 1;
    else if (g == '1')
      tmp.f[2][3] = 0, tmp.f[2][2] = 1;
    else if (g == '7')
      tmp.f[3][4] = 0, tmp.f[3][3] = 1;
    else if (g == '6')
      tmp.f[3][3] = tmp.f[4][4] = 1;
    return;
  }
  int M = (l + r) >> 1;
  ;
  build(rt << 1, l, M), build(rt << 1 | 1, M + 1, r);
  st[rt] = merge(st[rt << 1], st[rt << 1 | 1]);
}
node query(int rt, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    return st[rt];
  }
  int M = (l + r) >> 1;
  ;
  node gget;
  if (M >= L) gget = query(rt << 1, l, M, L, R);
  if (M < R) gget = merge(gget, query(rt << 1 | 1, M + 1, r, L, R));
  return gget;
}
void solve() {
  cin >> n >> m;
  cin >> k;
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int l, r;
    cin >> l >> r;
    ans = query(1, 1, n, l, r).f[0][4];
    cout << (ans == INF ? -1 : ans) << '\n';
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  while (T--) solve();
  return 0;
}
