#include <bits/stdc++.h>
using namespace std;
const int MX = 1 << 19;
const int INF = 1e9 + 5;
const long long MOD = 1e9 + 7;
long long fastPow(long long a, long long b, long long p = MOD) {
  long long res = 1;
  while (b) {
    if (b & 1LL) {
      res *= a;
      res %= p;
    }
    a *= a;
    a %= p;
    b >>= 1;
  }
  return res;
}
long long revMod(long long a, long long p = MOD) {
  return fastPow(a, p - 2, p);
}
int wart[] = {'2', '0', '1', '6'};
int bez[2 * MX][4][4];
char w[MX];
void policz(int v) {
  if (v >= MX) {
    for (int i = 0; i < 4; ++i) {
      for (int j = i; j < 4; ++j) {
        if (i == j)
          bez[v][i][j] = (int)(w[v - MX] == wart[i]);
        else
          bez[v][i][j] = 0;
      }
    }
  } else {
    for (int i = 0; i < 4; ++i) {
      for (int j = i; j < 4; ++j) {
        bez[v][i][j] = INF;
        int pra = 2 * v + 1;
        int lew = 2 * v;
        for (int k = i; k <= j; ++k) {
          bez[v][i][j] = min(bez[v][i][j], bez[lew][i][k] + bez[pra][k][j]);
        }
      }
    }
  }
}
vector<int> rozbij(int a, int b) {
  a += MX;
  b += MX;
  vector<int> lewo, prawo;
  lewo.push_back(a);
  if (a != b) prawo.push_back(b);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) lewo.push_back(a + 1);
    if (b % 2 == 1) prawo.push_back(b - 1);
    a /= 2;
    b /= 2;
  }
  for (int i = (int)prawo.size() - 1; i >= 0; --i) lewo.push_back(prawo[i]);
  return lewo;
}
int BEZ[4][4], NOWE[4][4];
int policz(int a, int b, int x, int y) {
  if (b < a) {
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) BEZ[i][j] = 0;
    return 0;
  }
  vector<int> xd = rozbij(a, b);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      BEZ[i][j] = bez[xd[0]][i][j];
    }
  }
  for (auto prz : xd)
    if (prz != xd[0]) {
      for (int i = 0; i < 4; ++i) {
        for (int j = i; j < 4; ++j) {
          NOWE[i][j] = INF;
          for (int k = i; k <= j; ++k) {
            NOWE[i][j] = min(NOWE[i][j], BEZ[i][k] + bez[prz][k][j]);
          }
        }
      }
      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
          BEZ[i][j] = NOWE[i][j];
        }
      }
    }
  return BEZ[x][y];
}
int n, m, xd[10000];
int wcz2[MX], wcz0[MX], wcz1[MX], wcz6[MX], wcz7[MX];
int XD[4][4];
int solve() {
  scanf("%d%d%s", &n, &m, w);
  for (int i = 2 * MX - 1; i >= 1; --i) {
    policz(i);
  }
  for (int i = 0; i < 10000; ++i) xd[i] = -1;
  for (int i = 0; i < n; ++i) {
    xd[(int)w[i]] = i;
    wcz2[i] = xd['2'];
    wcz0[i] = xd['0'];
    wcz1[i] = xd['1'];
    wcz6[i] = xd['6'];
    wcz7[i] = xd['7'];
  }
  while (m--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    int pos4 = wcz7[b];
    if (pos4 < a) {
      puts("-1");
      continue;
    }
    int pos3 = wcz1[pos4];
    if (pos3 < a) {
      puts("-1");
      continue;
    }
    int pos2 = wcz0[pos3];
    if (pos2 < a) {
      puts("-1");
      continue;
    }
    int pos1 = wcz2[pos2];
    if (pos1 < a) {
      puts("-1");
      continue;
    }
    policz(a, pos1 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) XD[i][j] = BEZ[i][j];
    XD[0][0] = INF;
    policz(pos1 + 1, pos2 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) {
        NOWE[i][j] = INF;
        for (int k = i; k <= j; ++k) {
          NOWE[i][j] = min(NOWE[i][j], XD[i][k] + BEZ[k][j]);
        }
      }
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) XD[i][j] = NOWE[i][j];
    XD[0][0] = INF;
    XD[0][1] = INF;
    XD[1][1] = INF;
    policz(pos2 + 1, pos3 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) {
        NOWE[i][j] = INF;
        for (int k = i; k <= j; ++k) {
          NOWE[i][j] = min(NOWE[i][j], XD[i][k] + BEZ[k][j]);
        }
      }
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) XD[i][j] = NOWE[i][j];
    printf("%d\n", XD[0][3] + policz(pos3, b, 3, 3));
  }
  return 0;
}
int main() {
  solve();
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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 MX = 1 << 19;
const int INF = 1e9 + 5;
const long long MOD = 1e9 + 7;
long long fastPow(long long a, long long b, long long p = MOD) {
  long long res = 1;
  while (b) {
    if (b & 1LL) {
      res *= a;
      res %= p;
    }
    a *= a;
    a %= p;
    b >>= 1;
  }
  return res;
}
long long revMod(long long a, long long p = MOD) {
  return fastPow(a, p - 2, p);
}
int wart[] = {'2', '0', '1', '6'};
int bez[2 * MX][4][4];
char w[MX];
void policz(int v) {
  if (v >= MX) {
    for (int i = 0; i < 4; ++i) {
      for (int j = i; j < 4; ++j) {
        if (i == j)
          bez[v][i][j] = (int)(w[v - MX] == wart[i]);
        else
          bez[v][i][j] = 0;
      }
    }
  } else {
    for (int i = 0; i < 4; ++i) {
      for (int j = i; j < 4; ++j) {
        bez[v][i][j] = INF;
        int pra = 2 * v + 1;
        int lew = 2 * v;
        for (int k = i; k <= j; ++k) {
          bez[v][i][j] = min(bez[v][i][j], bez[lew][i][k] + bez[pra][k][j]);
        }
      }
    }
  }
}
vector<int> rozbij(int a, int b) {
  a += MX;
  b += MX;
  vector<int> lewo, prawo;
  lewo.push_back(a);
  if (a != b) prawo.push_back(b);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) lewo.push_back(a + 1);
    if (b % 2 == 1) prawo.push_back(b - 1);
    a /= 2;
    b /= 2;
  }
  for (int i = (int)prawo.size() - 1; i >= 0; --i) lewo.push_back(prawo[i]);
  return lewo;
}
int BEZ[4][4], NOWE[4][4];
int policz(int a, int b, int x, int y) {
  if (b < a) {
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) BEZ[i][j] = 0;
    return 0;
  }
  vector<int> xd = rozbij(a, b);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      BEZ[i][j] = bez[xd[0]][i][j];
    }
  }
  for (auto prz : xd)
    if (prz != xd[0]) {
      for (int i = 0; i < 4; ++i) {
        for (int j = i; j < 4; ++j) {
          NOWE[i][j] = INF;
          for (int k = i; k <= j; ++k) {
            NOWE[i][j] = min(NOWE[i][j], BEZ[i][k] + bez[prz][k][j]);
          }
        }
      }
      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
          BEZ[i][j] = NOWE[i][j];
        }
      }
    }
  return BEZ[x][y];
}
int n, m, xd[10000];
int wcz2[MX], wcz0[MX], wcz1[MX], wcz6[MX], wcz7[MX];
int XD[4][4];
int solve() {
  scanf("%d%d%s", &n, &m, w);
  for (int i = 2 * MX - 1; i >= 1; --i) {
    policz(i);
  }
  for (int i = 0; i < 10000; ++i) xd[i] = -1;
  for (int i = 0; i < n; ++i) {
    xd[(int)w[i]] = i;
    wcz2[i] = xd['2'];
    wcz0[i] = xd['0'];
    wcz1[i] = xd['1'];
    wcz6[i] = xd['6'];
    wcz7[i] = xd['7'];
  }
  while (m--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    int pos4 = wcz7[b];
    if (pos4 < a) {
      puts("-1");
      continue;
    }
    int pos3 = wcz1[pos4];
    if (pos3 < a) {
      puts("-1");
      continue;
    }
    int pos2 = wcz0[pos3];
    if (pos2 < a) {
      puts("-1");
      continue;
    }
    int pos1 = wcz2[pos2];
    if (pos1 < a) {
      puts("-1");
      continue;
    }
    policz(a, pos1 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) XD[i][j] = BEZ[i][j];
    XD[0][0] = INF;
    policz(pos1 + 1, pos2 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) {
        NOWE[i][j] = INF;
        for (int k = i; k <= j; ++k) {
          NOWE[i][j] = min(NOWE[i][j], XD[i][k] + BEZ[k][j]);
        }
      }
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) XD[i][j] = NOWE[i][j];
    XD[0][0] = INF;
    XD[0][1] = INF;
    XD[1][1] = INF;
    policz(pos2 + 1, pos3 - 1, 0, 0);
    for (int i = 0; i < 4; ++i)
      for (int j = i; j < 4; ++j) {
        NOWE[i][j] = INF;
        for (int k = i; k <= j; ++k) {
          NOWE[i][j] = min(NOWE[i][j], XD[i][k] + BEZ[k][j]);
        }
      }
    for (int i = 0; i < 4; ++i)
      for (int j = 0; j < 4; ++j) XD[i][j] = NOWE[i][j];
    printf("%d\n", XD[0][3] + policz(pos3, b, 3, 3));
  }
  return 0;
}
int main() {
  solve();
  return 0;
}

```