#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 10;
const long long INF = 1e12 + 10;
struct node {
  long long digit[10], res[4][4];
  node() {
    for (long long i = 0; i < 10; i++) {
      digit[i] = 0;
    }
    for (long long i = 0; i < 4; i++) {
      for (long long j = 0; j < 4; j++) {
        res[i][j] = INF;
      }
    }
  }
};
node segt[4 * MAXN];
char a[MAXN];
long long f(long long x) {
  if (x == 0) {
    return 2;
  }
  if (x == 1) {
    return 0;
  }
  if (x == 2) {
    return 1;
  }
  if (x == 3 || x == 4) {
    return 6;
  }
  return -1;
}
node merge_node(node x, node y) {
  node z;
  for (long long i = 0; i < 10; i++) {
    z.digit[i] += x.digit[i];
    z.digit[i] += y.digit[i];
  }
  for (long long i = 0; i < 4; i++) {
    for (long long j = i; j < 4; j++) {
      if (i == 3 && j == 3) {
        if (z.digit[7] > 0) {
          z.res[i][j] = z.digit[6];
        } else {
          z.res[i][j] = INF;
        }
        continue;
      }
      long long w = f(j + 1);
      if (w != -1) {
        z.res[i][j] = min(z.res[i][j], x.res[i][j] + y.digit[w]);
      }
      z.res[i][j] = min(z.res[i][j], y.res[i][j] + x.digit[f(i)]);
      for (long long u = i; u < j; u++) {
        z.res[i][j] = min(z.res[i][j], x.res[i][u] + y.res[u + 1][j]);
      }
    }
  }
  return z;
}
void build(long long id, long long l, long long r) {
  if (l == r) {
    segt[id].digit[a[l] - '0']++;
    if (a[l] == '2') {
      segt[id].res[0][0] = 0;
    }
    if (a[l] == '0') {
      segt[id].res[1][1] = 0;
    }
    if (a[l] == '1') {
      segt[id].res[2][2] = 0;
    }
    if (a[l] == '7') {
      segt[id].res[3][3] = 0;
    }
    return;
  }
  long long mid = (l + r) / 2;
  build(2 * id + 1, l, mid);
  build(2 * id + 2, mid + 1, r);
  segt[id] = merge_node(segt[2 * id + 1], segt[2 * id + 2]);
}
node get_res(long long id, long long L, long long R, long long l, long long r) {
  if (L > r || R < l) {
    node res;
    return res;
  }
  if (L >= l && R <= r) {
    return segt[id];
  }
  long long mid = (L + R) / 2;
  return merge_node(get_res(2 * id + 1, L, mid, l, r),
                    get_res(2 * id + 2, mid + 1, R, l, r));
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  long long n, q;
  cin >> n >> q;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(0, 1, n);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    long long k = get_res(0, 1, n, l, r).res[0][3];
    if (k == INF) {
      k = -1;
    }
    cout << k << endl;
  }
}

### Prompt

Please formulate 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 long long MAXN = 2e5 + 10;
const long long INF = 1e12 + 10;
struct node {
  long long digit[10], res[4][4];
  node() {
    for (long long i = 0; i < 10; i++) {
      digit[i] = 0;
    }
    for (long long i = 0; i < 4; i++) {
      for (long long j = 0; j < 4; j++) {
        res[i][j] = INF;
      }
    }
  }
};
node segt[4 * MAXN];
char a[MAXN];
long long f(long long x) {
  if (x == 0) {
    return 2;
  }
  if (x == 1) {
    return 0;
  }
  if (x == 2) {
    return 1;
  }
  if (x == 3 || x == 4) {
    return 6;
  }
  return -1;
}
node merge_node(node x, node y) {
  node z;
  for (long long i = 0; i < 10; i++) {
    z.digit[i] += x.digit[i];
    z.digit[i] += y.digit[i];
  }
  for (long long i = 0; i < 4; i++) {
    for (long long j = i; j < 4; j++) {
      if (i == 3 && j == 3) {
        if (z.digit[7] > 0) {
          z.res[i][j] = z.digit[6];
        } else {
          z.res[i][j] = INF;
        }
        continue;
      }
      long long w = f(j + 1);
      if (w != -1) {
        z.res[i][j] = min(z.res[i][j], x.res[i][j] + y.digit[w]);
      }
      z.res[i][j] = min(z.res[i][j], y.res[i][j] + x.digit[f(i)]);
      for (long long u = i; u < j; u++) {
        z.res[i][j] = min(z.res[i][j], x.res[i][u] + y.res[u + 1][j]);
      }
    }
  }
  return z;
}
void build(long long id, long long l, long long r) {
  if (l == r) {
    segt[id].digit[a[l] - '0']++;
    if (a[l] == '2') {
      segt[id].res[0][0] = 0;
    }
    if (a[l] == '0') {
      segt[id].res[1][1] = 0;
    }
    if (a[l] == '1') {
      segt[id].res[2][2] = 0;
    }
    if (a[l] == '7') {
      segt[id].res[3][3] = 0;
    }
    return;
  }
  long long mid = (l + r) / 2;
  build(2 * id + 1, l, mid);
  build(2 * id + 2, mid + 1, r);
  segt[id] = merge_node(segt[2 * id + 1], segt[2 * id + 2]);
}
node get_res(long long id, long long L, long long R, long long l, long long r) {
  if (L > r || R < l) {
    node res;
    return res;
  }
  if (L >= l && R <= r) {
    return segt[id];
  }
  long long mid = (L + R) / 2;
  return merge_node(get_res(2 * id + 1, L, mid, l, r),
                    get_res(2 * id + 2, mid + 1, R, l, r));
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  long long n, q;
  cin >> n >> q;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(0, 1, n);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    long long k = get_res(0, 1, n, l, r).res[0][3];
    if (k == INF) {
      k = -1;
    }
    cout << k << endl;
  }
}

```