#include <bits/stdc++.h>
const int N = 250000;
const int M = 5;
struct Node {
  int cost[M][M];
  Node() {
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < M; ++j) {
        cost[i][j] = (int(1e9));
      }
    }
  }
};
Node node[N << 2];
int n, q;
std::string str;
std::map<char, int> my_index;
void init() {
  my_index['2'] = 1;
  my_index['0'] = 2;
  my_index['1'] = 3;
  my_index['7'] = 4;
}
void push_up(int rt) {
  for (int i = 0; i < M; ++i) {
    for (int j = i; j < M; ++j) {
      for (int k = j; k < M; ++k) {
        node[rt].cost[i][k] =
            std::min(node[rt].cost[i][k],
                     node[rt << 1].cost[i][j] + node[rt << 1 | 1].cost[j][k]);
      }
    }
  }
}
void build(int l, int r, int rt) {
  if (l == r) {
    char ch = str[l];
    for (int i = 0; i < M; ++i) {
      node[rt].cost[i][i] = 0;
    }
    if (my_index.count(ch)) {
      int id = my_index[ch];
      node[rt].cost[id - 1][id] = 0;
      node[rt].cost[id - 1][id - 1] = 1;
      node[rt].cost[id][id] = 0;
    } else if (ch == '6') {
      node[rt].cost[3][3] = 1;
      node[rt].cost[4][4] = 1;
    }
    return;
  }
  int m = (l + r) >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  push_up(rt);
}
Node merge(const Node& ret_l, const Node& ret_r) {
  Node ret;
  for (int i = 0; i < M; ++i) {
    for (int j = i; j < M; ++j) {
      for (int k = j; k < M; ++k) {
        ret.cost[i][k] =
            std::min(ret.cost[i][k], ret_l.cost[i][j] + ret_r.cost[j][k]);
      }
    }
  }
  return ret;
}
Node query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) {
    return node[rt];
  }
  int m = (l + r) >> 1;
  Node ret_l, ret_r, ret;
  bool left = false;
  if (L <= m) {
    ret_l = query(L, R, l, m, rt << 1);
    left = true;
    ret = ret_l;
  }
  if (R > m) {
    ret_r = query(L, R, m + 1, r, rt << 1 | 1);
    if (left)
      ret = merge(ret_l, ret_r);
    else
      ret = ret_r;
  }
  return ret;
}
int main() {
  std::cin >> n >> q >> str;
  init();
  build(0, n - 1, 1);
  while (q--) {
    int l, r;
    std::cin >> l >> r;
    Node ret = query(l - 1, r - 1, 0, n - 1, 1);
    int ans = (ret.cost[0][4] == (int(1e9))) ? -1 : ret.cost[0][4];
    std::cout << ans << std::endl;
  }
  return 0;
}
