#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 50;
int n;
struct data {
  int a[5][5];
};
data tree[4 * N];
data single_merge(data left, data right) {
  if (right.a[0][0] == n + 10) {
    return left;
  }
  if (left.a[0][0] == n + 10) {
    return right;
  }
  data ret;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      ret.a[i][j] = -1;
    }
  }
  for (int already_done = 0; already_done < 5; already_done++) {
    for (int will_be_done = already_done; will_be_done < 5; will_be_done++) {
      for (int left_done = already_done; left_done <= will_be_done;
           left_done++) {
        if (left.a[already_done][left_done] == -1 ||
            right.a[left_done][will_be_done] == -1) {
          continue;
        }
        if (ret.a[already_done][will_be_done] == -1) {
          ret.a[already_done][will_be_done] = left.a[already_done][left_done] +
                                              right.a[left_done][will_be_done];
        } else {
          ret.a[already_done][will_be_done] =
              min(ret.a[already_done][will_be_done],
                  left.a[already_done][left_done] +
                      right.a[left_done][will_be_done]);
        }
      }
    }
  }
  return ret;
}
data initialize(char c) {
  string ans = "2017";
  data ret;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      if (j < i) {
        ret.a[i][j] = -1;
      } else if (c == '6' && i >= 3 && j == i) {
        ret.a[i][j] = 1;
      } else if (j == i + 1 && c == ans[j - 1]) {
        ret.a[i][j] = 0;
      } else if (j == i) {
        if (j < 4 && c == ans[j]) {
          ret.a[i][j] = 1;
        } else {
          ret.a[i][j] = 0;
        }
      } else {
        ret.a[i][j] = -1;
      }
    }
  }
  return ret;
}
void build(string *s, int id = 1, int l = 0, int r = n) {
  if (r - l < 2) {
    tree[id] = initialize((*s)[l]);
    return;
  }
  int mid = (l + r) / 2;
  int left = 2 * id;
  int right = left + 1;
  build(s, left, l, mid);
  build(s, right, mid, r);
  tree[id] = single_merge(tree[left], tree[right]);
}
data query(int x, int y, int id = 1, int l = 0, int r = n) {
  if (r <= x || l > y) {
    data ret;
    ret.a[0][0] = n + 10;
    return ret;
  }
  if (l >= x && r - 1 <= y) {
    return tree[id];
  }
  int mid = (l + r) / 2;
  int left = 2 * id;
  int right = left + 1;
  return single_merge(query(x, y, left, l, mid), query(x, y, right, mid, r));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  string s;
  cin >> n >> q;
  cin >> s;
  build(&s);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    data ans = query(x, y);
    cout << ans.a[0][4] << '\n';
  }
}
