#include <bits/stdc++.h>
using namespace std;
const int infty = 100000000;
class int2 {
 public:
  int x;
  int2(int n = 0) : x(min(n, infty)) {}
  int2(const int2& i) : x(i.x) {}
} infty2(infty);
int2 operator+(const int2& a, const int2& b) {
  return int2(min(infty, a.x + b.x));
}
bool operator<(const int2& a, const int2& b) {
  if (a.x >= infty)
    return false;
  else if (b.x >= infty)
    return true;
  else
    return a.x < b.x;
}
bool operator==(const int2& a, const int2& b) {
  return (a.x == infty || b.x == infty) ? false : a.x == b.x;
}
bool is_infty(const int2& a) { return a.x >= infty; }
ostream& operator<<(ostream& os, const int2& n) {
  return os << (n.x >= infty ? -1 : n.x);
}
istream& operator>>(istream& is, int2& n) {
  is >> (n.x);
  if (n.x > infty) n.x = infty;
  return is;
}
typedef int2 Mat[5][5];
void display(const int2 m[5][5], ostream& os) {
  os << "[[" << m[0][0];
  for (int j = 1; j < 5; ++j) os << "," << m[0][j];
  for (int i = 1; i < 5; ++i) {
    os << "], [" << m[i][0];
    for (int j = 1; j < 5; ++j) os << "," << m[i][j];
  }
  os << "]]";
}
void get_mat(int2 m[5][5], char ch) {
  static const char str2017[] = "2017";
  static const char str20166[] = "20166";
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 5; ++j) m[i][j] = infty2;
  for (int i = 0; i < 5; ++i) {
    if (ch == str20166[i])
      m[i][i] = 1;
    else
      m[i][i] = 0;
  }
  for (int i = 0; i < 4; ++i)
    if (ch == str2017[i]) m[i][i + 1] = 0;
}
void copy(int2 dest[5][5], const int2 src[5][5]) {
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 5; ++j) dest[i][j] = src[i][j];
}
void join(int2 m[5][5], const int2 ml[5][5], const int2 mr[5][5]) {
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 5; ++j) {
      m[i][j] = infty2;
      for (int k = i; k <= j; ++k) m[i][j] = min(m[i][j], ml[i][k] + mr[k][j]);
    }
}
struct node {
  int2 m[5][5];
  explicit node(char ch = '9') { get_mat(m, ch); }
  node(const node& n) { copy(m, n.m); }
  node(const node& l, const node& r) { join(m, l.m, r.m); }
};
ostream& operator<<(ostream& os, const node& n) {
  display(n.m, os);
  return os;
}
node segtree[1 << 19];
char s[200001];
void init(int root, int first, int last) {
  if (first == last)
    segtree[root] = node(s[first]);
  else {
    int left = root + root + 1;
    int mid = (first + last) / 2;
    init(left, first, mid);
    init(left + 1, mid + 1, last);
    segtree[root] = node(segtree[left], segtree[left + 1]);
  }
}
void display(int root, int first, int last, int indent = 0,
             ostream& os = cerr) {
  for (int i = 0; i < indent; ++i) os << "  ";
  os << segtree[root] << endl;
  if (first != last) {
    int left = root + root + 1;
    int mid = (first + last) / 2;
    display(left, first, mid, indent + 1);
    display(left + 1, mid + 1, last, indent + 1);
  }
}
node query(int root, int first, int last, int i, int j) {
  if (j < first || last < i)
    return node();
  else if (i <= first && last <= j)
    return segtree[root];
  else {
    int left = root + root + 1;
    int mid = (first + last) / 2;
    node nl = query(left, first, mid, i, j);
    node nr = query(left + 1, mid + 1, last, i, j);
    return node(nl, nr);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  cin >> s;
  init(0, 0, n - 1);
  while (q-- > 0) {
    int first, last;
    cin >> first >> last;
    first--;
    last--;
    node nd = query(0, 0, n - 1, first, last);
    cout << (nd.m[0][4]) << endl;
  }
  return 0;
}
