#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int INF = 1e9;
typedef int mat[6][6];
vector<pair<char, int>> adj[8];
mat ONE;
mat single[300];
void mul(mat a, mat b, mat c) {
  static mat res;
  for (int i = 0; i < (int)(6); i++)
    for (int j = 0; j < (int)(6); j++) res[i][j] = INF;
  for (int i = 0; i < (int)(6); i++)
    for (int k = 0; k < (int)(6); k++)
      for (int j = 0; j < (int)(6); j++)
        res[i][j] = min(res[i][j], a[i][k] + b[k][j]);
  for (int i = 0; i < (int)(6); i++)
    for (int j = 0; j < (int)(6); j++) c[i][j] = res[i][j];
}
mat st[2 * N];
int n;
string s;
void print(mat a) {
  for (int i = 0; i < (int)(6); i++) {
    for (int j = 0; j < (int)(6); j++)
      if (a[i][j] >= INF)
        cerr << "oo ";
      else
        cerr << a[i][j] << ' ';
    cerr << endl;
  }
}
void query(int l, int r, mat a) {
  mat b;
  for (int i = 0; i < (int)(6); i++)
    for (int j = 0; j < (int)(6); j++) a[i][j] = b[i][j] = ONE[i][j];
  l += n;
  r += n;
  while (l < r) {
    if (l & 1) mul(a, st[l++], a);
    if (r & 1) mul(st[--r], b, b);
    l /= 2;
    r /= 2;
  }
  mul(a, b, a);
}
int main() {
  adj[0].emplace_back('2', 1);
  adj[1].emplace_back('0', 2);
  adj[2].emplace_back('1', 3);
  adj[3].emplace_back('7', 4);
  adj[3].emplace_back('6', 5);
  adj[4].emplace_back('6', 5);
  for (int i = 0; i < (int)(6); i++)
    for (int j = 0; j < (int)(6); j++) ONE[i][j] = (i == j ? 0 : INF);
  for (char c = '0'; c <= '9'; c++) {
    for (int i = 0; i < (int)(6); i++)
      for (int j = 0; j < (int)(6); j++) single[c][i][j] = (i == j ? 1 : INF);
    for (int i = 0; i < (int)(6); i++) {
      bool any = false;
      for (auto xy : adj[i])
        if (xy.first == c) {
          single[c][i][xy.second] = 0;
          any = true;
        }
      if (!any) single[c][i][i] = 0;
    }
  }
  int q;
  scanf("%d %d", &n, &q);
  cin >> s;
  for (int i = 0; i < (int)(n); i++) {
    for (int x = 0; x < (int)(6); x++)
      for (int y = 0; y < (int)(6); y++) st[n + i][x][y] = single[s[i]][x][y];
  }
  for (int i = (int)(n)-1; i >= ((int)1); i--)
    mul(st[2 * i], st[2 * i + 1], st[i]);
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    l--;
    static mat qu;
    query(l, r, qu);
    auto res = qu[0][4];
    if (res >= n) res = -1;
    printf("%d\n", res);
  }
  return 0;
}
