#include <bits/stdc++.h>
using namespace std;
int n, q;
string s;
const int INF = 0x3f;
struct Martix {
  int a[5][5];
  Martix() {
    memset(a, INF, sizeof(a));
    a[0][0] = a[1][1] = a[2][2] = a[3][3] = a[4][4] = 0;
  }
  Martix operator+(const Martix b) const {
    Martix res;
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        res.a[i][j] = 1061109567;
        for (int k = 0; k < 5; k++) {
          res.a[i][j] = min(res.a[i][j], a[i][k] + b.a[k][j]);
        }
      }
    }
    return res;
  }
};
Martix sum[1048577];
void build(int k, int l, int r) {
  if (l == r) {
    if (s[l] == '2') {
      sum[k].a[0][0] = 1;
      sum[k].a[0][1] = 0;
    } else if (s[l] == '0') {
      sum[k].a[1][1] = 1;
      sum[k].a[1][2] = 0;
    } else if (s[l] == '1') {
      sum[k].a[2][2] = 1;
      sum[k].a[2][3] = 0;
    } else if (s[l] == '7') {
      sum[k].a[3][3] = 1;
      sum[k].a[3][4] = 0;
    } else if (s[l] == '6') {
      sum[k].a[3][3] = 1;
      sum[k].a[4][4] = 1;
    }
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  sum[k] = sum[k << 1] + sum[k << 1 | 1];
}
Martix query(int k, int l, int r, int lef, int rig) {
  if (l >= lef && rig >= r) {
    return sum[k];
  }
  int mid = l + r >> 1;
  if (rig <= mid) return query(k << 1, l, mid, lef, rig);
  if (lef > mid) return query(k << 1 | 1, mid + 1, r, lef, rig);
  return query(k << 1, l, mid, lef, rig) +
         query(k << 1 | 1, mid + 1, r, lef, rig);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  cin >> s;
  s = "*" + s;
  build(1, 1, n);
  int l, r;
  while (q--) {
    cin >> l >> r;
    int ans = query(1, 1, n, l, r).a[0][4];
    if (ans == 1061109567) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}
