#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1000 * 100 + 10, T = 5;
int n;
string s, t1 = "2016", t2 = "2017";
struct Node {
  int dp[T][T];
  Node() {
    for (int i = 0; i < T; i++)
      for (int j = 0; j < T; j++) dp[i][j] = (i ^ j ? N + 10 : 0);
  }
  void init(char c) {
    for (int i = 0; i < T; i++)
      for (int j = 0; j < T; j++) dp[i][j] = (i ^ j ? N + 10 : 0);
    for (int i = 0; i < 4; i++)
      if (c == t2[i]) dp[i][i + 1] = 0, dp[i][i] = 1;
    if (c == '6') dp[3][3] = dp[4][4] = 1;
  }
} seg[N << 2];
Node merge(Node L, Node R) {
  Node res = Node();
  for (int i = 0; i < T; i++) res.dp[i][i] = N + 10;
  for (int i = 0; i < T; i++)
    for (int j = i; j < T; j++)
      for (int k = i; k <= j; k++)
        res.dp[i][j] = min(res.dp[i][j], L.dp[i][k] + R.dp[k][j]);
  return res;
}
void build(int l = 0, int r = n, int root = 1) {
  if (l + 1 == r) {
    seg[root].init(s[l]);
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, root << 1), build(mid, r, root << 1 | 1);
  seg[root] = merge(seg[root << 1], seg[root << 1 | 1]);
}
Node get(int s, int e, int l = 0, int r = n, int root = 1) {
  if (s <= l && e >= r) return seg[root];
  if (s >= r || e <= l) return Node();
  int mid = l + r >> 1;
  return merge(get(s, e, l, mid, root << 1), get(s, e, mid, r, root << 1 | 1));
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int Q;
  cin >> n >> Q >> s;
  for (int i = 0; i < N << 2; i++) seg[i] = Node();
  build();
  while (Q--) {
    int l, r;
    cin >> l >> r;
    int res = get(--l, r).dp[0][4];
    cout << (res < N ? res : -1) << '\n';
  }
  return 0;
}
