#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int mlog = 20;
const int SQ = 400;
const int maxk = 101;
struct Node {
  int a[4][4];
  Node() {
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) a[i][j] = 0;
  }
};
int n, q;
string st;
Node seg[maxn * 4];
Node merge(Node x, Node y) {
  Node res;
  for (int i = 0; i < 4; i++)
    for (int j = i; j < 4; j++) {
      res.a[i][j] = maxn;
      for (int k = i; k <= j; k++)
        res.a[i][j] = min(res.a[i][j], x.a[i][k] + y.a[k][j]);
    }
  return res;
}
void BUILD(int s = 0, int e = n, int id = 1) {
  if (e - s < 2) {
    if (st[s] == '2') seg[id].a[0][0] = 1;
    if (st[s] == '0') seg[id].a[1][1] = 1;
    if (st[s] == '1') seg[id].a[2][2] = 1;
    if (st[s] == '6') seg[id].a[3][3] = 1;
    return;
  }
  int mid = (s + e) / 2;
  BUILD(s, mid, id * 2 + 0);
  BUILD(mid, e, id * 2 + 1);
  seg[id] = merge(seg[id * 2 + 0], seg[id * 2 + 1]);
}
Node QUERY(int l, int r, int s = 0, int e = n, int id = 1) {
  if (l >= e || r <= s) return {};
  if (l <= s && r >= e) return seg[id];
  int mid = (s + e) / 2;
  return merge(QUERY(l, r, s, mid, id * 2 + 0),
               QUERY(l, r, mid, e, id * 2 + 1));
}
set<int> ss[10];
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
  ;
  cin >> n >> q;
  cin >> st;
  for (int i = 0; i < n; i++) ss[st[i] - '0'].insert(i);
  BUILD();
  while (q--) {
    int id1, id2, id3, id4;
    int l, r;
    cin >> l >> r;
    l--;
    auto it = ss[7].lower_bound(r);
    if (it == ss[7].begin()) {
      cout << "-1\n";
      continue;
    }
    it--;
    id1 = *it;
    it = ss[1].lower_bound(id1);
    if (it == ss[1].begin()) {
      cout << "-1\n";
      continue;
    }
    it--;
    id2 = *it;
    it = ss[0].lower_bound(id2);
    if (it == ss[0].begin()) {
      cout << "-1\n";
      continue;
    }
    it--;
    id3 = *it;
    it = ss[2].lower_bound(id3);
    if (it == ss[2].begin()) {
      cout << "-1\n";
      continue;
    }
    it--;
    id4 = *it;
    if (id4 < l) {
      cout << "-1\n";
      continue;
    }
    Node p1, p2, p3;
    p1 = QUERY(id3, id2);
    p2 = QUERY(id4, id3);
    p3 = QUERY(l, id4);
    int ans =
        min(p1.a[3][3] + min({p2.a[1][3] + p3.a[0][1], p2.a[2][3] + p3.a[0][2],
                              p2.a[3][3] + p3.a[0][3]}),
            p1.a[2][3] + min(p2.a[1][2] + p3.a[0][1], p2.a[2][2] + p3.a[0][2]));
    cout << ans + QUERY(id1, r).a[3][3] + QUERY(id2, id1).a[3][3] << "\n";
  }
  return 0;
}
