#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const string year = "2017";
const int len = 5;
struct matrix {
  int val[len][len];
  void reset() { fill(&val[0][0], &val[0][0] + len * len, inf); }
  matrix() {
    reset();
    for (int i = 0; i < int(len); ++i) val[i][i] = 0;
  }
  matrix(int digit) {
    *this = matrix();
    assert(len == (int)(year).size() + 1);
    int pos = find((year).begin(), (year).end(), digit + '0') - year.begin();
    if (pos != (int)(year).size()) {
      val[pos][pos] = 1;
      val[pos][pos + 1] = 0;
    } else if (digit == 6) {
      val[3][3] = 1;
      val[4][4] = 1;
    }
  }
};
matrix operator*(const matrix &l, const matrix &r) {
  matrix ans;
  ans.reset();
  for (int i = 0; i < int(len); ++i)
    for (int j = 0; j < int(len); ++j)
      for (int k = 0; k < int(len); ++k)
        ans.val[i][k] = min(ans.val[i][k], l.val[i][j] + r.val[j][k]);
  return ans;
}
struct segtree {
  vector<matrix> data;
  int tsz;
  segtree(const string &s) {
    tsz = 1;
    while (tsz < (int)(s).size()) tsz *= 2;
    data.assign(2 * tsz, matrix());
    for (int i = 0; i < int((int)(s).size()); ++i)
      data[tsz + i] = matrix(s[i] - '0');
    for (int i = tsz - 1; i >= 1; i--) data[i] = data[2 * i] * data[2 * i + 1];
  }
  matrix query(int l, int r) {
    matrix ansl = matrix(), ansr = matrix();
    for (l += tsz, r += tsz; l < r; l >>= 1, r >>= 1) {
      if (l & 1) ansl = ansl * data[l++];
      if (r & 1) ansr = data[--r] * ansr;
    }
    return ansl * ansr;
  }
};
void solve(int n) {
  int q;
  cin >> q;
  string s;
  cin >> s;
  assert((int)(s).size() == n);
  segtree data(s);
  for (int i = 0; i < int(q); ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    matrix res = data.query(l, r);
    int ans = (res.val[0][4] == inf ? -1 : res.val[0][4]);
    cout << ans << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (cin >> n) solve(n);
}
