#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433832795l;
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
  return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int K = 5;
const int INF = 1e9;
struct node {
  int t[K][K];
  node() {
    for (int i = int(0); i < int(K); ++i)
      for (int j = int(0); j < int(K); ++j) t[i][j] = INF;
    for (int i = int(0); i < int(K); ++i) t[i][i] = 0;
  }
};
const int N = 200100;
node t[4 * N];
int sz;
node botva(node a, node b) {
  node c;
  for (int i = int(0); i < int(K); ++i)
    for (int j = int(i); j < int(K); ++j) {
      c.t[i][j] = INF;
      for (int k = int(i); k < int(j + 1); ++k)
        umn(c.t[i][j], a.t[i][k] + b.t[k][j]);
    }
  return c;
}
node fnd(int i, int x, int y, int l, int r) {
  if (x > r || y < l) return node();
  if (x >= l && y <= r) {
    return t[i];
  }
  return botva(fnd(2 * i, x, (x + y) >> 1, l, r),
               fnd(2 * i + 1, ((x + y) >> 1) + 1, y, l, r));
}
node fnd(int l, int r) {
  l += sz, r += sz;
  if (l == r) {
    return t[l];
  }
  node left = t[l], right = t[r];
  while (l / 2 != r / 2) {
    if (!(l & 1)) {
      left = botva(left, t[l + 1]);
    }
    if (r & 1) {
      right = botva(t[r - 1], right);
    }
    l >>= 1, r >>= 1;
  }
  return botva(left, right);
}
char c[N];
int main() {
  cout.setf(ios::showpoint | ios::fixed);
  cout.precision(20);
  int n, q;
  cin >> n >> q;
  string s;
  getline(cin, s);
  gets(c);
  s = c;
  string g = "2017";
  sz = 1;
  while (sz < n) sz <<= 1;
  for (int i = int(0); i < int(n); ++i) {
    node& v = t[i + sz];
    for (int j = int(0); j < int(((int)(g).size())); ++j) {
      if (g[j] == s[i]) {
        v.t[j][j] = 1;
        v.t[j][j + 1] = 0;
      }
    }
    if (s[i] == '6') {
      v.t[3][3] = v.t[4][4] = 1;
    }
  }
  for (int i = int(sz) - 1; i >= int(1); --i) {
    t[i] = botva(t[2 * i], t[2 * i + 1]);
  }
  for (int i = int(0); i < int(q); ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--, r--;
    node res = fnd(1, 0, sz - 1, l, r);
    if (res.t[0][4] >= INF) {
      printf("-1\n");
    } else {
      printf("%d\n", res.t[0][4]);
    }
  }
  return 0;
}
