#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
char s[maxn];
struct Ans {
  int dp[4][4];
};
struct {
  int L, R;
  int pl, pr;
  Ans ans;
} T[2 * maxn];
int buildTree(int L, int R) {
  static int sz = 0;
  int cur = sz++;
  T[cur].L = L;
  T[cur].R = R;
  if (L < R) {
    T[cur].pl = buildTree(L, (L + R) / 2);
    T[cur].pr = buildTree((L + R) / 2 + 1, R);
    for (int l = 0; l < 4; l++) {
      for (int r = l; r < 4; r++) {
        T[cur].ans.dp[l][r] = maxn;
        for (int k = l; k <= r; k++) {
          T[cur].ans.dp[l][r] =
              min(T[cur].ans.dp[l][r],
                  T[T[cur].pl].ans.dp[l][k] + T[T[cur].pr].ans.dp[k][r]);
        }
      }
    }
  } else {
    if (s[L] == '2') {
      T[cur].ans.dp[0][0] = 1;
    } else if (s[L] == '0') {
      T[cur].ans.dp[1][1] = 1;
    } else if (s[L] == '1') {
      T[cur].ans.dp[2][2] = 1;
    } else if (s[L] == '6') {
      T[cur].ans.dp[3][3] = 1;
    }
  }
  return cur;
}
Ans get(int L, int R, int cur) {
  if (L <= T[cur].L && R >= T[cur].R) return T[cur].ans;
  Ans ansL, ansR;
  for (int l = 0; l < 4; l++) {
    for (int r = l; r < 4; r++) {
      ansL.dp[l][r] = ansR.dp[l][r] = 0;
    }
  }
  if (L <= T[T[cur].pl].R && T[T[cur].pl].L <= R) ansL = get(L, R, T[cur].pl);
  if (L <= T[T[cur].pr].R && T[T[cur].pr].L <= R) ansR = get(L, R, T[cur].pr);
  Ans ans;
  for (int l = 0; l < 4; l++) {
    for (int r = l; r < 4; r++) {
      ans.dp[l][r] = maxn;
      for (int k = l; k <= r; k++) {
        ans.dp[l][r] = min(ans.dp[l][r], ansL.dp[l][k] + ansR.dp[k][r]);
      }
    }
  }
  return ans;
}
int prev2[maxn];
int prev0[maxn];
int prev1[maxn];
int prev7[maxn];
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", s);
  prev2[0] = prev0[0] = prev1[0] = prev7[0] = -1;
  if (s[0] == '2') prev2[0] = 0;
  if (s[0] == '0') prev0[0] = 0;
  if (s[0] == '1') prev1[0] = 0;
  if (s[0] == '7') prev7[0] = 0;
  for (int i = 1; i < n; i++) {
    prev2[i] = prev2[i - 1];
    prev0[i] = prev0[i - 1];
    prev1[i] = prev1[i - 1];
    prev7[i] = prev7[i - 1];
    if (s[i] == '2') prev2[i] = i;
    if (s[i] == '0') prev0[i] = i;
    if (s[i] == '1') prev1[i] = i;
    if (s[i] == '7') prev7[i] = i;
  }
  buildTree(0, n - 1);
  for (int Q = 0; Q < q; Q++) {
    int l, r;
    scanf("%d %d", &l, &r);
    l--, r--;
    int pos2, pos0, pos1, pos7;
    pos7 = prev7[r];
    if (pos7 < l) {
      printf("-1\n");
      continue;
    }
    pos1 = prev1[pos7];
    if (pos1 < l) {
      printf("-1\n");
      continue;
    }
    pos0 = prev0[pos1];
    if (pos0 < l) {
      printf("-1\n");
      continue;
    }
    pos2 = prev2[pos0];
    if (pos2 < l) {
      printf("-1\n");
      continue;
    }
    Ans ans1, ans2, ans3;
    for (int l = 0; l < 4; l++)
      for (int r = l; r < 4; r++)
        ans1.dp[l][r] = ans2.dp[l][r] = ans3.dp[l][r] = 0;
    if (pos2 - 1 >= l) ans1 = get(l, pos2 - 1, 0);
    ans2 = get(pos1 + 1, r, 0);
    if (pos0 - 1 >= pos2 + 1) ans3 = get(pos2 + 1, pos0 - 1, 0);
    int ans = maxn;
    int tmp1, tmp2, tmp3, tmp4;
    tmp1 = tmp2 = tmp3 = tmp4 = 0;
    tmp1 = ans1.dp[0][3];
    ans = min(ans, tmp1 + get(pos2 + 1, r, 0).dp[3][3]);
    tmp1 = tmp2 = tmp3 = 0;
    tmp1 = ans1.dp[0][2];
    ans = min(ans, tmp1 + get(pos2 + 1, pos1 - 1, 0).dp[2][3] + ans2.dp[3][3]);
    tmp1 = ans1.dp[0][1];
    tmp2 = ans3.dp[1][3];
    ans = min(ans, tmp1 + tmp2 + get(pos0 + 1, r, 0).dp[3][3]);
    tmp2 = 0;
    tmp2 = ans3.dp[1][2];
    if (pos1 - 1 >= pos0 + 1) tmp3 = get(pos0 + 1, pos1 - 1, 0).dp[2][3];
    tmp4 = ans2.dp[3][3];
    ans = min(ans, tmp1 + tmp2 + tmp3 + tmp4);
    printf("%d\n", ans);
  }
}

### Prompt

Your challenge is to write a cpp solution to the following problem:

A string t is called nice if a string "2017" occurs in t as a subsequence but a string "2016" doesn't occur in t as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.

The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is  - 1.

Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).

Input

The first line of the input contains two integers n and q (4 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the length of the string s and the number of queries respectively.

The second line contains a string s of length n. Every character is one of digits '0'–'9'.

The i-th of next q lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ n), describing a substring in the i-th query.

Output

For each query print the ugliness of the given substring.

Examples

Input

8 3
20166766
1 8
1 7
2 8


Output

4
3
-1


Input

15 5
012016662091670
3 4
1 14
4 15
1 13
10 15


Output

-1
2
1
-1
-1


Input

4 2
1234
2 4
1 2


Output

-1
-1

Note

In the first sample:

  * In the first query, ugliness("20166766") = 4 because all four sixes must be removed. 
  * In the second query, ugliness("2016676") = 3 because all three sixes must be removed. 
  * In the third query, ugliness("0166766") = - 1 because it's impossible to remove some digits to get a nice string. 



In the second sample:

  * In the second query, ugliness("01201666209167") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice. 
  * In the third query, ugliness("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170". 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
char s[maxn];
struct Ans {
  int dp[4][4];
};
struct {
  int L, R;
  int pl, pr;
  Ans ans;
} T[2 * maxn];
int buildTree(int L, int R) {
  static int sz = 0;
  int cur = sz++;
  T[cur].L = L;
  T[cur].R = R;
  if (L < R) {
    T[cur].pl = buildTree(L, (L + R) / 2);
    T[cur].pr = buildTree((L + R) / 2 + 1, R);
    for (int l = 0; l < 4; l++) {
      for (int r = l; r < 4; r++) {
        T[cur].ans.dp[l][r] = maxn;
        for (int k = l; k <= r; k++) {
          T[cur].ans.dp[l][r] =
              min(T[cur].ans.dp[l][r],
                  T[T[cur].pl].ans.dp[l][k] + T[T[cur].pr].ans.dp[k][r]);
        }
      }
    }
  } else {
    if (s[L] == '2') {
      T[cur].ans.dp[0][0] = 1;
    } else if (s[L] == '0') {
      T[cur].ans.dp[1][1] = 1;
    } else if (s[L] == '1') {
      T[cur].ans.dp[2][2] = 1;
    } else if (s[L] == '6') {
      T[cur].ans.dp[3][3] = 1;
    }
  }
  return cur;
}
Ans get(int L, int R, int cur) {
  if (L <= T[cur].L && R >= T[cur].R) return T[cur].ans;
  Ans ansL, ansR;
  for (int l = 0; l < 4; l++) {
    for (int r = l; r < 4; r++) {
      ansL.dp[l][r] = ansR.dp[l][r] = 0;
    }
  }
  if (L <= T[T[cur].pl].R && T[T[cur].pl].L <= R) ansL = get(L, R, T[cur].pl);
  if (L <= T[T[cur].pr].R && T[T[cur].pr].L <= R) ansR = get(L, R, T[cur].pr);
  Ans ans;
  for (int l = 0; l < 4; l++) {
    for (int r = l; r < 4; r++) {
      ans.dp[l][r] = maxn;
      for (int k = l; k <= r; k++) {
        ans.dp[l][r] = min(ans.dp[l][r], ansL.dp[l][k] + ansR.dp[k][r]);
      }
    }
  }
  return ans;
}
int prev2[maxn];
int prev0[maxn];
int prev1[maxn];
int prev7[maxn];
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", s);
  prev2[0] = prev0[0] = prev1[0] = prev7[0] = -1;
  if (s[0] == '2') prev2[0] = 0;
  if (s[0] == '0') prev0[0] = 0;
  if (s[0] == '1') prev1[0] = 0;
  if (s[0] == '7') prev7[0] = 0;
  for (int i = 1; i < n; i++) {
    prev2[i] = prev2[i - 1];
    prev0[i] = prev0[i - 1];
    prev1[i] = prev1[i - 1];
    prev7[i] = prev7[i - 1];
    if (s[i] == '2') prev2[i] = i;
    if (s[i] == '0') prev0[i] = i;
    if (s[i] == '1') prev1[i] = i;
    if (s[i] == '7') prev7[i] = i;
  }
  buildTree(0, n - 1);
  for (int Q = 0; Q < q; Q++) {
    int l, r;
    scanf("%d %d", &l, &r);
    l--, r--;
    int pos2, pos0, pos1, pos7;
    pos7 = prev7[r];
    if (pos7 < l) {
      printf("-1\n");
      continue;
    }
    pos1 = prev1[pos7];
    if (pos1 < l) {
      printf("-1\n");
      continue;
    }
    pos0 = prev0[pos1];
    if (pos0 < l) {
      printf("-1\n");
      continue;
    }
    pos2 = prev2[pos0];
    if (pos2 < l) {
      printf("-1\n");
      continue;
    }
    Ans ans1, ans2, ans3;
    for (int l = 0; l < 4; l++)
      for (int r = l; r < 4; r++)
        ans1.dp[l][r] = ans2.dp[l][r] = ans3.dp[l][r] = 0;
    if (pos2 - 1 >= l) ans1 = get(l, pos2 - 1, 0);
    ans2 = get(pos1 + 1, r, 0);
    if (pos0 - 1 >= pos2 + 1) ans3 = get(pos2 + 1, pos0 - 1, 0);
    int ans = maxn;
    int tmp1, tmp2, tmp3, tmp4;
    tmp1 = tmp2 = tmp3 = tmp4 = 0;
    tmp1 = ans1.dp[0][3];
    ans = min(ans, tmp1 + get(pos2 + 1, r, 0).dp[3][3]);
    tmp1 = tmp2 = tmp3 = 0;
    tmp1 = ans1.dp[0][2];
    ans = min(ans, tmp1 + get(pos2 + 1, pos1 - 1, 0).dp[2][3] + ans2.dp[3][3]);
    tmp1 = ans1.dp[0][1];
    tmp2 = ans3.dp[1][3];
    ans = min(ans, tmp1 + tmp2 + get(pos0 + 1, r, 0).dp[3][3]);
    tmp2 = 0;
    tmp2 = ans3.dp[1][2];
    if (pos1 - 1 >= pos0 + 1) tmp3 = get(pos0 + 1, pos1 - 1, 0).dp[2][3];
    tmp4 = ans2.dp[3][3];
    ans = min(ans, tmp1 + tmp2 + tmp3 + tmp4);
    printf("%d\n", ans);
  }
}

```