#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
class MyNode {
 public:
  int ugliness[5][5];
  bool empty;
  MyNode();
  MyNode(bool);
  MyNode merge(const MyNode &w);
};
class LazyValue {
 private:
  long long value;
  bool empty;

 public:
  LazyValue() { empty = true; }
  LazyValue(long long x) { empty = false, value = x; }
  bool is_empty() { return empty; }
  LazyValue accumulate(int a, int b, int l, int r, const LazyValue &lv);
  MyNode update_node(int a, int b, int l, int r, const MyNode &nd);
};
class SegTree {
 private:
  int N;
  MyNode *segTree;
  LazyValue *lazy;
  void change(int pos, int a, int b, int l, int r, LazyValue lv) {
    segTree[pos] = lv.update_node(a, b, l, r, segTree[pos]);
    lazy[pos] = lazy[pos].accumulate(a, b, l, r, lv);
  }
  void to_propagate(int pos, int a, int b, int l, int r) {
    if (!lazy[pos].is_empty()) {
      int mid = (a + b) >> 1;
      change(2 * pos, a, mid, l, r, lazy[pos]);
      change(2 * pos + 1, mid + 1, b, l, r, lazy[pos]);
      lazy[pos] = LazyValue();
    }
  }
  MyNode st_search(int pos, int a, int b, int l, int r) {
    if (l <= a && b <= r) {
      return segTree[pos];
    }
    int med = (a + b) >> 1;
    MyNode res;
    to_propagate(pos, a, b, l, r);
    if (l <= med) res = res.merge(st_search(2 * pos, a, med, l, r));
    if (r > med) res = res.merge(st_search(2 * pos + 1, med + 1, b, l, r));
    return res;
  }
  void st_update(int pos, int a, int b, int l, int r, const LazyValue &lv) {
    if (l <= a && b <= r) {
      return change(pos, a, b, l, r, lv);
    }
    int med = (a + b) >> 1;
    to_propagate(pos, a, b, l, r);
    if (l <= med) st_update(2 * pos, a, med, l, r, lv);
    if (r > med) st_update(2 * pos + 1, med + 1, b, l, r, lv);
    segTree[pos] = segTree[2 * pos].merge(segTree[2 * pos + 1]);
  }
  void init(MyNode *v, int pos, int a, int b) {
    if (a == b) {
      segTree[pos] = v[a];
    } else {
      int med = (a + b) >> 1;
      init(v, 2 * pos, a, med);
      init(v, 2 * pos + 1, med + 1, b);
      segTree[pos] = segTree[2 * pos].merge(segTree[2 * pos + 1]);
    }
  }

 public:
  SegTree(MyNode *v, int _n) {
    N = _n;
    segTree = new MyNode[4 * N];
    lazy = new LazyValue[4 * N];
    init(v, 1, 0, N - 1);
  }
  SegTree(int _n) {
    N = _n;
    segTree = new MyNode[4 * N];
    lazy = new LazyValue[4 * N];
  }
  ~SegTree() {
    delete[] segTree;
    delete[] lazy;
  }
  MyNode search(int l, int r) { return st_search(1, 0, N - 1, l, r); }
  void update(int l, int r, LazyValue lv) { st_update(1, 0, N - 1, l, r, lv); }
};
MyNode::MyNode() { empty = true; }
MyNode::MyNode(bool val) { empty = val; }
MyNode MyNode::merge(const MyNode &w) {
  if (empty) return w;
  if (w.empty) return *this;
  MyNode ans;
  ans.empty = false;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      ans.ugliness[i][j] = INF;
      for (int k = 0; k < 5; k++) {
        ans.ugliness[i][j] =
            min(ans.ugliness[i][j], ugliness[i][k] + w.ugliness[k][j]);
      }
    }
  }
  return ans;
}
LazyValue LazyValue::accumulate(int a, int b, int l, int r,
                                const LazyValue &lv) {
  LazyValue ans = LazyValue(lv.value);
  return ans;
}
MyNode LazyValue::update_node(int a, int b, int l, int r, const MyNode &nd) {
  MyNode ans = nd;
  return ans;
}
const int MAXN = 200005;
int n, q;
char s[MAXN];
MyNode ugly_vector[MAXN];
MyNode init(char c) {
  MyNode ans(false);
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      ans.ugliness[i][j] = INF;
      if (i == 0) {
        if (j == 0) {
          ans.ugliness[i][j] = (c == '2' ? 1 : 0);
        } else if (j == 1 && c == '2') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 1) {
        if (j == 1) {
          ans.ugliness[i][j] = (c == '0' ? 1 : 0);
        } else if (j == 2 && c == '0') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 2) {
        if (j == 2) {
          ans.ugliness[i][j] = (c == '1' ? 1 : 0);
        } else if (j == 3 && c == '1') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 3) {
        if (j == 3) {
          ans.ugliness[i][j] = (c == '7' || c == '6' ? 1 : 0);
        } else if (j == 4 && c == '7') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 4) {
        if (j == 4) {
          ans.ugliness[i][j] = (c == '6' ? 1 : 0);
        }
      }
    }
  }
  return ans;
}
void debug(MyNode nd) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      cout << nd.ugliness[i][j] << " ";
    }
    cout << endl;
  }
}
int main() {
  scanf(" %d %d", &n, &q);
  scanf(" %s", s);
  for (int i = 0; i < n; i++) {
    ugly_vector[i] = init(s[i]);
  }
  SegTree st(ugly_vector, n);
  for (int i = 0; i < q; i++) {
    int a, b;
    scanf(" %d %d", &a, &b);
    a--, b--;
    int ans = st.search(a, b).ugliness[0][4];
    if (ans >= INF) ans = -1;
    printf("%d\n", ans);
  }
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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 INF = 1000000000;
class MyNode {
 public:
  int ugliness[5][5];
  bool empty;
  MyNode();
  MyNode(bool);
  MyNode merge(const MyNode &w);
};
class LazyValue {
 private:
  long long value;
  bool empty;

 public:
  LazyValue() { empty = true; }
  LazyValue(long long x) { empty = false, value = x; }
  bool is_empty() { return empty; }
  LazyValue accumulate(int a, int b, int l, int r, const LazyValue &lv);
  MyNode update_node(int a, int b, int l, int r, const MyNode &nd);
};
class SegTree {
 private:
  int N;
  MyNode *segTree;
  LazyValue *lazy;
  void change(int pos, int a, int b, int l, int r, LazyValue lv) {
    segTree[pos] = lv.update_node(a, b, l, r, segTree[pos]);
    lazy[pos] = lazy[pos].accumulate(a, b, l, r, lv);
  }
  void to_propagate(int pos, int a, int b, int l, int r) {
    if (!lazy[pos].is_empty()) {
      int mid = (a + b) >> 1;
      change(2 * pos, a, mid, l, r, lazy[pos]);
      change(2 * pos + 1, mid + 1, b, l, r, lazy[pos]);
      lazy[pos] = LazyValue();
    }
  }
  MyNode st_search(int pos, int a, int b, int l, int r) {
    if (l <= a && b <= r) {
      return segTree[pos];
    }
    int med = (a + b) >> 1;
    MyNode res;
    to_propagate(pos, a, b, l, r);
    if (l <= med) res = res.merge(st_search(2 * pos, a, med, l, r));
    if (r > med) res = res.merge(st_search(2 * pos + 1, med + 1, b, l, r));
    return res;
  }
  void st_update(int pos, int a, int b, int l, int r, const LazyValue &lv) {
    if (l <= a && b <= r) {
      return change(pos, a, b, l, r, lv);
    }
    int med = (a + b) >> 1;
    to_propagate(pos, a, b, l, r);
    if (l <= med) st_update(2 * pos, a, med, l, r, lv);
    if (r > med) st_update(2 * pos + 1, med + 1, b, l, r, lv);
    segTree[pos] = segTree[2 * pos].merge(segTree[2 * pos + 1]);
  }
  void init(MyNode *v, int pos, int a, int b) {
    if (a == b) {
      segTree[pos] = v[a];
    } else {
      int med = (a + b) >> 1;
      init(v, 2 * pos, a, med);
      init(v, 2 * pos + 1, med + 1, b);
      segTree[pos] = segTree[2 * pos].merge(segTree[2 * pos + 1]);
    }
  }

 public:
  SegTree(MyNode *v, int _n) {
    N = _n;
    segTree = new MyNode[4 * N];
    lazy = new LazyValue[4 * N];
    init(v, 1, 0, N - 1);
  }
  SegTree(int _n) {
    N = _n;
    segTree = new MyNode[4 * N];
    lazy = new LazyValue[4 * N];
  }
  ~SegTree() {
    delete[] segTree;
    delete[] lazy;
  }
  MyNode search(int l, int r) { return st_search(1, 0, N - 1, l, r); }
  void update(int l, int r, LazyValue lv) { st_update(1, 0, N - 1, l, r, lv); }
};
MyNode::MyNode() { empty = true; }
MyNode::MyNode(bool val) { empty = val; }
MyNode MyNode::merge(const MyNode &w) {
  if (empty) return w;
  if (w.empty) return *this;
  MyNode ans;
  ans.empty = false;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      ans.ugliness[i][j] = INF;
      for (int k = 0; k < 5; k++) {
        ans.ugliness[i][j] =
            min(ans.ugliness[i][j], ugliness[i][k] + w.ugliness[k][j]);
      }
    }
  }
  return ans;
}
LazyValue LazyValue::accumulate(int a, int b, int l, int r,
                                const LazyValue &lv) {
  LazyValue ans = LazyValue(lv.value);
  return ans;
}
MyNode LazyValue::update_node(int a, int b, int l, int r, const MyNode &nd) {
  MyNode ans = nd;
  return ans;
}
const int MAXN = 200005;
int n, q;
char s[MAXN];
MyNode ugly_vector[MAXN];
MyNode init(char c) {
  MyNode ans(false);
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      ans.ugliness[i][j] = INF;
      if (i == 0) {
        if (j == 0) {
          ans.ugliness[i][j] = (c == '2' ? 1 : 0);
        } else if (j == 1 && c == '2') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 1) {
        if (j == 1) {
          ans.ugliness[i][j] = (c == '0' ? 1 : 0);
        } else if (j == 2 && c == '0') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 2) {
        if (j == 2) {
          ans.ugliness[i][j] = (c == '1' ? 1 : 0);
        } else if (j == 3 && c == '1') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 3) {
        if (j == 3) {
          ans.ugliness[i][j] = (c == '7' || c == '6' ? 1 : 0);
        } else if (j == 4 && c == '7') {
          ans.ugliness[i][j] = 0;
        }
      } else if (i == 4) {
        if (j == 4) {
          ans.ugliness[i][j] = (c == '6' ? 1 : 0);
        }
      }
    }
  }
  return ans;
}
void debug(MyNode nd) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      cout << nd.ugliness[i][j] << " ";
    }
    cout << endl;
  }
}
int main() {
  scanf(" %d %d", &n, &q);
  scanf(" %s", s);
  for (int i = 0; i < n; i++) {
    ugly_vector[i] = init(s[i]);
  }
  SegTree st(ugly_vector, n);
  for (int i = 0; i < q; i++) {
    int a, b;
    scanf(" %d %d", &a, &b);
    a--, b--;
    int ans = st.search(a, b).ugliness[0][4];
    if (ans >= INF) ans = -1;
    printf("%d\n", ans);
  }
}

```