#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 7;
struct data {
  int dp[5][5];
  data() {
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        dp[i][j] = INF;
      }
      dp[i][i] = 0;
    }
  }
  data(int x) {
    for (int i = 0; i < 5; i++) {
      int go = -1;
      if (i == 0) {
        if (x == 2) {
          go = 1;
        } else {
          go = 0;
        }
      } else if (i == 1) {
        if (x == 0) {
          go = 2;
        } else {
          go = 1;
        }
      } else if (i == 2) {
        if (x == 1) {
          go = 3;
        } else {
          go = 2;
        }
      } else if (i == 3) {
        if (x == 7) {
          go = 4;
        } else if (x == 6) {
          go = -1;
        } else {
          go = 3;
        }
      } else {
        if (x == 6) {
          go = -1;
        } else {
          go = 4;
        }
      }
      for (int j = 0; j < 5; j++) {
        if (j == go) {
          dp[i][j] = 0;
        } else if (j == i) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = INF;
        }
      }
    }
  }
};
data empt = data();
data merge(const data x, const data y) {
  data z;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      z.dp[i][j] = INF;
    }
  }
  for (int i = 0; i < 5; i++) {
    for (int j = i; j < 5; j++) {
      for (int k = i; k <= j; k++) {
        z.dp[i][j] = min(z.dp[i][j], x.dp[i][k] + y.dp[k][j]);
      }
    }
  }
  return z;
}
const int MAXN = 200 * 1000 + 7;
data t[4 * MAXN];
char a[MAXN];
void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = data(a[tl - 1] - '0');
  } else {
    int tm = (tl + tr) >> 1;
    build(v + v, tl, tm);
    build(v + v + 1, tm + 1, tr);
    t[v] = merge(t[v + v], t[v + v + 1]);
  }
}
data get(int v, int tl, int tr, int l, int r) {
  if (r < tl || l > tr) return empt;
  if (tl >= l && tr <= r) return t[v];
  int tm = (tl + tr) >> 1;
  return merge(get(v + v, tl, tm, l, r), get(v + v + 1, tm + 1, tr, l, r));
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", a);
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    data x = get(1, 1, n, l, r);
    if (x.dp[0][4] == INF) {
      printf("-1\n");
    } else {
      printf("%d\n", x.dp[0][4]);
    }
  }
}
