#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 1e9;
struct node {
  int dp[5][5];
  node() {
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        dp[i][j] = inf;
      }
    }
  }
};
int n, m;
node seg[maxn << 2];
string st, f = "2017 ", f2 = "20166";
node merge(node l, node r) {
  node c;
  for (int i = 0; i < 5; i++) {
    for (int j = i; j < 5; j++) {
      for (int k = j; k < 5; k++) {
        c.dp[i][k] = min(c.dp[i][k], l.dp[i][j] + r.dp[j][k]);
      }
    }
  }
  return c;
}
void bld(int id, int s, int e) {
  if (e - s == 1) {
    for (int i = 0; i < 5; i++) {
      if (st[s] == f[i]) {
        seg[id].dp[i][i + 1] = 0;
        seg[id].dp[i][i] = 1;
      } else if (st[s] == f2[i]) {
        seg[id].dp[i][i] = 1;
      } else {
        seg[id].dp[i][i] = 0;
      }
    }
    return;
  }
  int mid = (s + e) >> 1;
  bld((id << 1), s, mid);
  bld(((id << 1) | 1), mid, e);
  seg[id] = merge(seg[(id << 1)], seg[((id << 1) | 1)]);
  return;
}
node get(int id, int s, int e, int l, int r) {
  if (l <= s && e <= r) return seg[id];
  int mid = (s + e) >> 1;
  if (r <= mid) return get((id << 1), s, mid, l, r);
  if (l >= mid) return get(((id << 1) | 1), mid, e, l, r);
  return merge(get((id << 1), s, mid, l, r),
               get(((id << 1) | 1), mid, e, l, r));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m >> st;
  bld(1, 0, n);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    int x = get(1, 0, n, l - 1, r).dp[0][4];
    if (x == inf) x = -1;
    cout << x << "\n";
  }
  return 0;
}
