#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 200005;
 int x[maxn], n, m;
 int main() {
   int ans = 0, use = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
     cin >> m;
     x[m] = 0;
   }
   for (int i = 1; i <= n; i++) {
     cin >> m;
     x[m] = i;
   }
   for (int i = 1; x[i] <= n && i <= n && x[i + 1] == x[i] + 1; i++) use = i + 1;
   if (use && x[use] == n) {
     m = 0;
     for (int i = use + 1; i <= n; i++) m = max(m, x[i] - (i - use - 1));
     if (!m) {
       cout << n - use <<
 ;
       return 0;
     }
   }
   if (x[1] && x[1] < n) {
     for (int i = 2; i <= n; i++) x[i] = max(0, x[i] - x[1]);
     ans += x[1];
     x[1] = 0;
   }
   if (x[1] == n) {
     m = 0;
     for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 2));
     if (m) {
       cout << ans + 2 * n <<
 ;
       return 0;
     } else {
       cout << ans + n - 1 <<
 ;
       return 0;
     }
   }
   m = 0;
   for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 1));
   cout << ans + m + n <<
 ;
   return 0;
 }


#include <bits/stdc++.h>
 const long long int mod = 1000000007;
 using namespace std;
 void toh_chaliye_shuru_karte_hain() {}
 void code_samapti_ki_ghoshna() {}
 long long int power(long long int a, long long int b, long long int modi) {
   a %= modi;
   long long int res = 1;
   while (b) {
     if (b % 2) {
       res = (res * a) % modi;
     }
     b /= 2;
     a = (a * a) % modi;
   }
   return res;
 }
 int main() {
   toh_chaliye_shuru_karte_hain();
   ios::sync_with_stdio(0);
   cin.tie(0);
   int n, x1, y1, x2, y2;
   cin >> n >> x1 >> y1 >> x2 >> y2;
   if (x1 == 0 && x2 == 0 || x1 == n && x2 == n) {
     cout << abs(y1 - y2) <<
 ;
   } else if (y1 == 0 && y2 == 0 || y1 == n && y2 == n) {
     cout << abs(x1 - x2) <<
 ;
   } else {
     int ans = 0;
     ans += min(n - y1 + n - y2, y1 + y2);
     ans += min(n - x1 + n - x2, x1 + x2);
     cout << ans <<
 ;
   }
   code_samapti_ki_ghoshna();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct segment_change {
   static const int SENTINEL = numeric_limits<int>::min();
   int to_set;
   segment_change(int _to_set = SENTINEL) : to_set(_to_set) {}
   bool has_set() const { return to_set != SENTINEL; }
   void reset() { to_set = SENTINEL; }
   bool has_change() const { return has_set(); }
   segment_change combine(const segment_change &other) const {
     return other.has_set() ? other : *this;
   }
 };
 struct segment {
   int first;
   segment(int _first = -1) : first(_first) {}
   void apply(int, const segment_change &change) {
     if (change.has_set()) first = change.to_set;
   }
   void join(const segment &other) {
     if (first < 0) first = other.first;
   }
   void join(const segment &a, const segment &b) {
     *this = a;
     join(b);
   }
 };
 pair<int, int> right_half[32];
 struct seg_tree {
   int tree_n = 0;
   vector<segment> tree;
   vector<segment_change> changes;
   seg_tree(int n = -1) {
     if (n >= 0) init(n);
   }
   void init(int n) {
     tree_n = 1;
     while (tree_n < n) tree_n *= 2;
     tree.assign(2 * tree_n, segment());
     changes.assign(tree_n, segment_change());
   }
   void build(const vector<segment> &initial) {
     int n = initial.size();
     init(n);
     assert(n <= tree_n);
     for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i];
     for (int position = tree_n - 1; position > 0; position--)
       tree[position].join(tree[2 * position], tree[2 * position + 1]);
   }
   void push_down(int position, int length) {
     if (!changes[position].has_change()) return;
     if (2 * position < tree_n) {
       changes[2 * position] = changes[2 * position].combine(changes[position]);
       changes[2 * position + 1] =
           changes[2 * position + 1].combine(changes[position]);
     }
     tree[2 * position].apply(length / 2, changes[position]);
     tree[2 * position + 1].apply(length / 2, changes[position]);
     changes[position].reset();
   }
   void push_all(int a, int b) {
     assert(0 <= a && a < b && b <= tree_n);
     a += tree_n;
     b += tree_n - 1;
     for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) {
       int x = a >> up, y = b >> up;
       push_down(x, 1 << up);
       if (x != y) push_down(y, 1 << up);
     }
   }
   void join_and_apply(int position, int length) {
     tree[position].join(tree[2 * position], tree[2 * position + 1]);
     tree[position].apply(length, changes[position]);
   }
   void join_all(int a, int b) {
     assert(0 <= a && a < b && b <= tree_n);
     a += tree_n;
     b += tree_n - 1;
     int length = 1;
     while (a > 1) {
       a /= 2;
       b /= 2;
       length *= 2;
       join_and_apply(a, length);
       if (a != b) join_and_apply(b, length);
     }
   }
   template <typename T_range_op>
   void process_range(int a, int b, bool needs_join, T_range_op &&range_op) {
     if (a == b) return;
     push_all(a, b);
     int original_a = a, original_b = b;
     int length = 1, r_size = 0;
     for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) {
       if (a & 1) range_op(a++, length);
       if (b & 1) right_half[r_size++] = {--b, length};
     }
     for (int i = r_size - 1; i >= 0; i--)
       range_op(right_half[i].first, right_half[i].second);
     if (needs_join) join_all(original_a, original_b);
   }
   segment query(int a, int b) {
     assert(0 <= a && a <= b && b <= tree_n);
     segment answer;
     process_range(a, b, false,
                   [&](int position, int) { answer.join(tree[position]); });
     return answer;
   }
   void update(int a, int b, const segment_change &change) {
     assert(0 <= a && a <= b && b <= tree_n);
     process_range(a, b, true, [&](int position, int length) {
       tree[position].apply(length, change);
       if (position < tree_n)
         changes[position] = changes[position].combine(change);
     });
   }
 };
 static segment v_half[32];
 struct ordered_path_heavy_light {
   int n = 0;
   bool vertex_mode;
   vector<vector<int>> adj;
   vector<int> parent, depth, subtree_size;
   vector<int> chain_root, chain_size;
   vector<seg_tree> chain_up_tree, chain_down_tree;
   ordered_path_heavy_light() {}
   ordered_path_heavy_light(int _n, bool _vertex_mode) {
     init(_n, _vertex_mode);
   }
   void init(int _n, bool _vertex_mode) {
     n = _n;
     vertex_mode = _vertex_mode;
     adj.assign(n, {});
     parent.resize(n);
     depth.resize(n);
     subtree_size.resize(n);
     chain_root.resize(n);
     chain_size.assign(n, 0);
     chain_up_tree.assign(n, {});
     chain_down_tree.assign(n, {});
   }
   void add_edge(int a, int b) {
     adj[a].push_back(b);
     adj[b].push_back(a);
   }
   void dfs(int node, int par) {
     parent[node] = par;
     depth[node] = par < 0 ? 0 : depth[par] + 1;
     subtree_size[node] = 1;
     if (par >= 0)
       adj[node].erase(find(adj[node].begin(), adj[node].end(), par));
     for (int &child : adj[node]) {
       dfs(child, node);
       subtree_size[node] += subtree_size[child];
     }
     sort(adj[node].begin(), adj[node].end(),
          [&](int a, int b) { return subtree_size[a] > subtree_size[b]; });
   }
   void chain_dfs(int node, bool heavy) {
     chain_root[node] = heavy ? chain_root[parent[node]] : node;
     chain_size[chain_root[node]]++;
     bool heavy_child = true;
     for (int child : adj[node]) {
       chain_dfs(child, heavy_child);
       heavy_child = false;
     }
   }
   void build(const segment &initial) {
     parent.assign(n, -1);
     for (int i = 0; i < n; i++)
       if (parent[i] < 0) {
         dfs(i, -1);
         chain_dfs(i, false);
       }
     for (int i = 0; i < n; i++)
       if (chain_root[i] == i) {
         chain_up_tree[i].init(chain_size[i]);
         chain_down_tree[i].init(chain_size[i]);
         vector<segment> initial_segs(chain_size[i], initial);
         chain_up_tree[i].build(initial_segs);
         chain_down_tree[i].build(initial_segs);
       }
   }
   segment _query_up_tree(int root, int start, int end) {
     int size = chain_size[root];
     return chain_up_tree[root].query(size - end, size - start);
   }
   void _update_up_tree(int root, int start, int end,
                        const segment_change &change) {
     int size = chain_size[root];
     chain_up_tree[root].update(size - end, size - start, change);
   }
   void _update_both_trees(int root, int start, int end,
                           const segment_change &change) {
     _update_up_tree(root, start, end, change);
     chain_down_tree[root].update(start, end, change);
   }
   segment query_path(int u, int v) {
     segment u_half;
     int v_size = 0;
     while (chain_root[u] != chain_root[v]) {
       bool pull_u = depth[chain_root[u]] > depth[chain_root[v]];
       int pull_node = pull_u ? u : v;
       int root = chain_root[pull_node];
       int end = depth[pull_node] - depth[root] + 1;
       if (pull_u) {
         u_half.join(_query_up_tree(root, 0, end));
         u = parent[root];
       } else {
         v_half[v_size++] = chain_down_tree[root].query(0, end);
         v = parent[root];
       }
     }
     bool pull_u = depth[u] > depth[v];
     int pull_node = pull_u ? u : v;
     int ancestor = pull_u ? v : u;
     int root = chain_root[pull_node];
     int start = depth[ancestor] - depth[root] + (vertex_mode ? 0 : 1);
     int end = depth[pull_node] - depth[root] + 1;
     if (pull_u)
       u_half.join(_query_up_tree(root, start, end));
     else
       v_half[v_size++] = chain_down_tree[root].query(start, end);
     for (int i = v_size - 1; i >= 0; i--) u_half.join(v_half[i]);
     return u_half;
   }
   void update_path(int u, int v, const segment_change &change) {
     while (chain_root[u] != chain_root[v]) {
       if (depth[chain_root[u]] > depth[chain_root[v]]) swap(u, v);
       int root = chain_root[v];
       _update_both_trees(root, 0, depth[v] - depth[root] + 1, change);
       v = parent[root];
     }
     if (depth[u] > depth[v]) swap(u, v);
     int root = chain_root[v];
     _update_both_trees(root, depth[u] - depth[root] + (vertex_mode ? 0 : 1),
                        depth[v] - depth[root] + 1, change);
   }
 };
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   int N;
   cin >> N;
   ordered_path_heavy_light HLD(N, false);
   vector<pair<int, int>> T1(N - 1);
   for (int i = 0; i < N - 1; i++) {
     int a, b;
     cin >> a >> b;
     a--;
     b--;
     HLD.add_edge(a, b);
     T1[i] = {a, b};
   }
   HLD.build(segment());
   for (int e = 0; e < N - 1; e++)
     HLD.update_path(T1[e].first, T1[e].second, segment_change(e));
   vector<vector<int>> adj(N);
   vector<int> degree(N, 0);
   for (int i = 0; i < N - 1; i++) {
     int a, b;
     cin >> a >> b;
     a--;
     b--;
     adj[a].push_back(b);
     adj[b].push_back(a);
     degree[a]++;
     degree[b]++;
   }
   queue<int> leaves;
   for (int i = 0; i < N; i++)
     if (degree[i] == 1) leaves.push(i);
   cout << N - 1 << '
';
   while (!leaves.empty()) {
     int leaf = leaves.front();
     leaves.pop();
     if (degree[leaf] == 0) break;
     int parent = -1;
     for (int neighbor : adj[leaf])
       if (degree[neighbor] != 0) parent = neighbor;
     assert(parent >= 0);
     int first_edge = HLD.query_path(leaf, parent).first;
     assert(first_edge >= 0);
     int x = T1[first_edge].first, y = T1[first_edge].second;
     HLD.update_path(x, y, segment_change(-1));
     degree[leaf]--;
     degree[parent]--;
     if (degree[parent] == 1) leaves.push(parent);
     cout << x + 1 << ' ' << y + 1 << ' ' << leaf + 1 << ' ' << parent + 1
          << '
';
   }
 }


#include <bits/stdc++.h>
 #pragma comment(linker,  /STACK:268435456 )
 using namespace std;
 const string IN_NAME =  input.txt ;
 const string OUT_NAME =  output.txt ;
 template <class T>
 T abs(T &x) {
   return ((x) >= 0) ? (x) : (-(x));
 }
 template <class T>
 T sqr(T &x) {
   return (x) * (x);
 }
 template <class T>
 T min(T &a, T &b) {
   return ((a) < (b)) ? (a) : (b);
 }
 template <class T>
 T max(T &a, T &b) {
   return ((a) > (b)) ? (a) : (b);
 }
 int n, m;
 struct tree {
   int x, h, pL, pR;
   tree() {}
   bool operator<(const tree &t) const { return x < t.x; }
 };
 struct mush {
   int x, z;
   mush() {}
   bool operator<(const mush &t) const { return x < t.x; }
 };
 tree a[100010];
 mush b[100010], tmpb[100010];
 int binSearch(int x) {
   if (x < b[0].x) return -1;
   if (x > b[m - 1].x) return -m - 1;
   int left = 0, right = m - 1;
   while (right - left > 1) {
     int mid = (left + right) >> 1;
     if (b[mid].x == x) return mid;
     if (b[mid].x < x)
       left = mid + 1;
     else
       right = mid - 1;
   }
   for (int i = max(0, left - 4); i < min(m - 1, right + 3); i++) {
     if (b[i].x < x && x < b[i + 1].x) return -(i + 1) - 1;
   }
   return right;
 }
 double d[100010];
 double db[100010];
 int dk;
 void multQuery(int left, int right, double p) {
   if (left > right) return;
   int L = left / dk + 1;
   int R = right / dk - 1;
   if (L > R) {
     for (int i = left; i <= right; i++) d[i] *= p;
   } else {
     for (int i = left; i < L * dk; i++) d[i] *= p;
     for (int j = L; j <= R; j++) db[j] *= p;
     for (int i = (R + 1) * dk; i <= right; i++) d[i] *= p;
   }
 }
 double getQuery(int index) { return d[index] * db[index / dk]; }
 int main() {
   scanf( %d%d , &n, &m);
   for (int i = 0; i < n; i++)
     scanf( %d%d%d%d , &a[i].x, &a[i].h, &a[i].pL, &a[i].pR);
   for (int i = 0; i < m; i++) scanf( %d%d , &tmpb[i].x, &tmpb[i].z);
   sort(a, a + n);
   sort(tmpb, tmpb + m);
   int newM = 0;
   for (int i = 0; i < m; i++) {
     if (i == 0 || tmpb[i - 1].x < tmpb[i].x) {
       b[newM].x = tmpb[i].x;
       b[newM].z = tmpb[i].z;
       newM++;
     } else {
       b[newM - 1].z += tmpb[i].z;
     }
   }
   m = newM;
   for (int i = 0; i < n; i++) a[i].x *= 2;
   for (int i = 0; i < n; i++) a[i].h *= 2;
   for (int i = 0; i < m; i++) b[i].x *= 2;
   dk = (int)sqrt(m + 1e-9);
   for (int i = 0; i < m + 5; i++) {
     d[i] = db[i] = 1;
   }
   for (int i = 0; i < n; i++) {
     int leftIndex = binSearch(a[i].x - a[i].h - 1);
     int rightIndex = binSearch(a[i].x + a[i].h + 1);
     int thisLeftIndex = binSearch(a[i].x - 1);
     int thisRightIndex = binSearch(a[i].x + 1);
     leftIndex = ~leftIndex;
     rightIndex = ~rightIndex;
     thisLeftIndex = ~thisLeftIndex;
     thisRightIndex = ~thisRightIndex;
     multQuery(leftIndex, thisLeftIndex - 1, 1.0 - a[i].pL / 100.0);
     multQuery(thisRightIndex, rightIndex - 1, 1.0 - a[i].pR / 100.0);
   }
   double result = 0;
   for (int i = 0; i < m; i++) {
     result += getQuery(i) * b[i].z;
   }
   printf( %.12f , result);
 }


#include <bits/stdc++.h>
 using namespace std;
 const double pi = acos(0.0) * 2.0;
 const double eps = 1e-12;
 const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
 template <class T>
 inline T abs1(T a) {
   return a < 0 ? -a : a;
 }
 template <class T>
 inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
   T ans = a > b ? a : b;
   if (c != -1234567) ans = max1(ans, c);
   if (d != -1234567) ans = max1(ans, d);
   return ans;
 }
 template <class T>
 inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
   T ans = a < b ? a : b;
   if (c != -1234567) ans = min(ans, c);
   if (d != -1234567) ans = min(ans, d);
   return ans;
 }
 template <class T>
 inline T gcd1(T a, T b) {
   if (a < b) swap(a, b);
   if (a % b == 0) return b;
   return gcd1(b, a % b);
 }
 template <class T>
 inline T lb(T num) {
   return num & (-num);
 }
 inline int jud(double a, double b) {
   if (abs1(a - b) < eps) return 0;
   if (a < b) return -1;
   return 1;
 }
 template <typename it, typename t1>
 inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
   if (na == 1) return 0;
   int be = 0, en = na - 1;
   if (*a <= *(a + na - 1)) {
     if (f_lb == 0)
       while (be < en) {
         int mid = (be + en + 1) / 2;
         if (jud(*(a + mid), val) != 1)
           be = mid;
         else
           en = mid - 1;
       }
     else
       while (be < en) {
         int mid = (be + en) / 2;
         if (jud(*(a + mid), val) != -1)
           en = mid;
         else
           be = mid + 1;
       }
     if (f_small && jud(*(a + be), val) == 1 && be != 0) be--;
     if (!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;
   } else {
     if (f_lb)
       while (be < en) {
         int mid = (be + en + 1) / 2;
         if (jud(*(a + mid), val) != -1)
           be = mid;
         else
           en = mid - 1;
       }
     else
       while (be < en) {
         int mid = (be + en) / 2;
         if (jud(*(a + mid), val) != 1)
           en = mid;
         else
           be = mid + 1;
       }
     if (!f_small && jud(*(a + be), val) == -1 && be != 0) be--;
     if (f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;
   }
   return be;
 }
 inline int bitnum(unsigned long long nValue) {
   nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) +
            (0x5555555555555555ull & nValue);
   nValue = ((0xccccccccccccccccull & nValue) >> 2) +
            (0x3333333333333333ull & nValue);
   nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) +
            (0x0f0f0f0f0f0f0f0full & nValue);
   nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) +
            (0x00ff00ff00ff00ffull & nValue);
   nValue = ((0xffff0000ffff0000ull & nValue) >> 16) +
            (0x0000ffff0000ffffull & nValue);
   nValue = ((0xffffffff00000000ull & nValue) >> 32) +
            (0x00000000ffffffffull & nValue);
   return nValue;
 }
 long long pow(long long n, long long m, long long mod = 0) {
   long long ans = 1;
   long long k = n;
   while (m) {
     if (m & 1) {
       ans *= k;
       if (mod) ans %= mod;
     }
     k *= k;
     if (mod) k %= mod;
     m >>= 1;
   }
   return ans;
 }
 const int maxn = 200110;
 char str[maxn];
 int f[maxn];
 int cntwen = 0, cnt = 0, st1 = 0;
 int main() {
   scanf( %s , str);
   int l = strlen(str);
   bool st = (str[0] >= 'A' && str[0] <= 'J');
   for (int i = 0; i < l; i++) {
     if (str[i] >= 'A' && str[i] <= 'J') {
       if (f[str[i] - 'A'] == 0) cnt++;
       f[str[i] - 'A']++;
     }
     if (str[i] == '?') cntwen++;
   }
   int ans = 1;
   for (int i = 0; i < cnt; i++) ans *= 10 - i;
   if (st) {
     ans /= 10;
     ans *= 9;
   }
   if (str[0] == '?') ans *= 9, cntwen--;
   printf( %d , ans);
   for (int i = 0; i < cntwen; i++) printf( 0 );
   printf(
 );
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 string toString(T x) {
   if (x == 0) return  0 ;
   bool negative = x < 0;
   string res;
   while (x) {
     res.push_back('0' + x % 10);
     x /= 10;
   }
   if (negative) res.push_back('-');
   reverse(res.begin(), res.end());
   return res;
 }
 template <typename T>
 T gcd(T a, T b) {
   return a == 0 ? b : gcd(b % a, a);
 }
 template <typename T>
 T lcm(T a, T b) {
   return a / gcd(a, b) * b;
 }
 int main() {
   string s;
   cin >> s;
   vector<int> a(s.length() / 2);
   int m;
   cin >> m;
   for (int i = 0; i < m; i++) {
     int t;
     cin >> t;
     t--;
     a[t]++;
   }
   int l = s.length() / 2;
   vector<bool> r(l);
   int carry = 0;
   for (int i = 0; i < l; i++) {
     int t = a[i] + carry;
     carry = t % 2;
     r[i] = carry;
   }
   for (int i = 0; i < l; i++) {
     if (r[i]) {
       cout << s[s.length() - 1 - i];
     } else {
       cout << s[i];
     }
   }
   if (s.length() % 2 == 1) {
     cout << s[l];
     l++;
   }
   for (int i = (s.length() + 1) / 2; i < s.length(); i++) {
     if (r[s.length() - 1 - i]) {
       cout << s[s.length() - 1 - i];
     } else {
       cout << s[i];
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, k, key, MK, MKEY;
 int MAP[110][110];
 int ii, jj;
 void dfssolve(int pos, int step) {
   int temp[110];
   int cnt, i, j;
   if (pos > n) {
     key = 1;
     k = k < step ? k : step;
     return;
   }
   cnt = 0;
   for (i = 1; i <= m; ++i) {
     temp[i] = MAP[pos][i];
     if (MAP[pos][i] != MAP[pos - 1][i]) {
       cnt++;
       MAP[pos][i] = MAP[pos - 1][i];
     }
   }
   if (step + cnt <= k) dfssolve(pos + 1, step + cnt);
   cnt = 0;
   for (i = 1; i <= m; ++i) {
     if (temp[i] == MAP[pos - 1][i]) {
       cnt++;
       MAP[pos][i] = MAP[pos - 1][i] & 1;
     }
   }
   if (step + cnt <= k) dfssolve(pos + 1, step + cnt);
   for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i];
 }
 void dfssolveup(int pos, int step) {
   int cnt, i, j;
   int temp[110];
   if (pos < 1) {
     MKEY = 1;
     MK = step < MK ? step : MK;
     return;
   }
   cnt = 0;
   for (i = 1; i <= m; ++i) {
     temp[i] = MAP[pos][i];
     if (MAP[pos][i] != MAP[pos + 1][i]) {
       cnt++;
       MAP[pos][i] = MAP[pos + 1][i];
     }
   }
   if (step + cnt <= k) dfssolveup(pos - 1, step + cnt);
   cnt = 0;
   for (i = 1; i <= m; ++i) {
     if (temp[i] == MAP[pos + 1][i]) {
       cnt++;
       MAP[pos][i] = MAP[pos + 1][i] & 1;
     }
   }
   if (step + cnt <= k) dfssolveup(pos - 1, step + cnt);
   for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i];
 }
 void dfs(int pos, int step) {
   if (k < step) return;
   if (pos == m) {
     dfssolve(2, step);
     MAP[1][pos] ^= 1;
     dfssolve(2, step + 1);
     MAP[1][pos] ^= 1;
   } else {
     dfs(pos + 1, step);
     MAP[1][pos] ^= 1;
     dfs(pos + 1, step + 1);
     MAP[1][pos] ^= 1;
   }
 }
 int main() {
   cin >> n >> m >> k;
   key = 0;
   if (n < m) {
     for (ii = 1; ii <= n; ++ii)
       for (jj = 1; jj <= m; ++jj) cin >> MAP[jj][ii];
     swap(n, m);
   } else {
     for (ii = 1; ii <= n; ++ii)
       for (jj = 1; jj <= m; ++jj) cin >> MAP[ii][jj];
   }
   if (n <= k) {
     dfs(1, 0);
   } else {
     for (ii = 1; ii <= n; ++ii) {
       MK = 10;
       MKEY = 0;
       dfssolveup(ii - 1, 0);
       if (MKEY) {
         dfssolve(ii + 1, MK);
       }
     }
   }
   if (key)
     cout << k << endl;
   else
     cout << -1 << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long mod = 1e9 + 7;
 const long long N = 1e5 + 5;
 long long ans = 1e18;
 map<long long, bool> mp;
 void solve(long long n, long long cnt) {
   if (!n) {
     cout << cnt;
     exit(0);
   }
   string s = to_string(n);
   sort(s.rbegin(), s.rend());
   solve(n - (s[0] - '0'), cnt + 1);
 }
 inline long long solve() {
   long long n;
   cin >> n;
   solve(n, 0);
   cout << ans;
   return 0;
 }
 signed main() {
   ios_base::sync_with_stdio(0);
   cin.tie(NULL);
   cout.tie(NULL);
   long long t = 1;
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, m, t, d;
   cin >> n >> m;
   map<long long, long long> mp;
   set<pair<long long, long long> > st;
   vector<long long> a(n), c(n);
   for (int i = 0; i < n; i++) {
     cin >> a[i];
     mp[i] = a[i];
   }
   for (int i = 0; i < n; i++) {
     cin >> c[i];
     st.insert({c[i], i});
   }
   for (int i = 0; i < m; i++) {
     cin >> t >> d;
     long long cost = 0;
     t--;
     if (st.size() == 0)
       cout << 0 << endl;
     else {
       if (mp[t] != 0) {
         if (mp[t] > d) {
           mp[t] -= d;
           cout << (long long)d * c[t] << endl;
           d = 0;
         } else if (mp[t] == d) {
           mp[t] -= d;
           cout << (long long)d * c[t] << endl;
           d = 0;
           st.erase({c[t], t});
         } else {
           cost += (long long)mp[t] * c[t];
           d -= mp[t];
           mp[t] = 0;
           st.erase({c[t], t});
         }
       }
       if (d > 0) {
         while (d > 0 && st.size() > 0) {
           pair<int, int> now = *st.begin();
           if (mp[now.second] > d) {
             mp[now.second] -= d;
             cout << (long long)cost + d * now.first << endl;
             d = 0;
           } else if (mp[now.second] == d) {
             mp[now.second] -= d;
             cout << (long long)cost + d * now.first << endl;
             d = 0;
             st.erase(st.begin());
           } else {
             cost += (long long)mp[now.second] * now.first;
             d -= mp[now.second];
             mp[now.second] = 0;
             st.erase(st.begin());
           }
         }
         if (d > 0) {
           cout << 0 << endl;
         }
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void pri(deque<int> arr) {
   for (int i : arr) cout << i <<    ;
   cout << endl;
 }
 int main() {
   string s;
   cin >> s;
   int n = s.size();
   if (n <= 3) {
     cout << s[0] << endl;
     return 0;
   }
   deque<int> a1;
   deque<int> b1;
   deque<int> c1;
   int as[n + 2];
   int bs[n + 2];
   int cs[n + 2];
   memset(as, 0, sizeof as);
   memset(bs, 0, sizeof bs);
   memset(cs, 0, sizeof cs);
   as[0] = 0;
   bs[0] = 0;
   cs[0] = 0;
   for (int i = 0; i < n; i++) {
     if (s[i] == 'a') {
       a1.push_back(i);
       as[i + 1]++;
     } else if (s[i] == 'b') {
       b1.push_back(i);
       bs[i + 1]++;
     } else {
       c1.push_back(i);
       cs[i + 1]++;
     }
     as[i + 1] += as[i];
     bs[i + 1] += bs[i];
     cs[i + 1] += cs[i];
   }
   int lim = -10;
   int mx = 1000006;
   string ans =   ;
   while (true) {
     int a = lim;
     int b = lim;
     int c = lim;
     if (a1.size() > 1) {
       a = a1[a1.size() - 1] - a1[0];
     }
     if (b1.size() > 1) {
       b = b1[b1.size() - 1] - b1[0];
     }
     if (c1.size() > 1) {
       c = c1.back() - c1.front();
     }
     int x = max(max(a, b), c);
     if (x == lim) break;
     int az = mx, bz = mx, cz = mx;
     if (a1.size() > 0) {
       az = as[a1[a1.size() - 1] + 1] - as[a1[0]];
     }
     if (b1.size() > 0) {
       bz = bs[b1[b1.size() - 1] + 1] - bs[b1[0]];
     }
     if (c1.size() > 0) {
       cz = cs[c1[c1.size() - 1] + 1] - cs[c1[0]];
     }
     if (x > a) {
       a = lim;
       az = mx;
     }
     if (x > b) {
       b = lim;
       bz = mx;
     }
     if (x > c) {
       c = lim;
       cz = mx;
     }
     x += min(min(az, bz), cz);
     a += az;
     b += bz;
     c += cz;
     if (x == a) {
       ans +=  a ;
       while (b1.size() > 0 && b1[0] < a1[0]) b1.pop_front();
       while (b1.size() > 0 && b1[b1.size() - 1] > a1[a1.size() - 1])
         b1.pop_back();
       while (c1.size() > 0 && c1[0] < a1[0]) c1.pop_front();
       while (c1.size() > 0 && c1[c1.size() - 1] > a1[a1.size() - 1])
         c1.pop_back();
       a1.pop_front();
       a1.pop_back();
     } else if (x == b) {
       ans +=  b ;
       while (a1.size() > 0 && a1[0] < b1[0]) a1.pop_front();
       while (a1.size() > 0 && a1[a1.size() - 1] > b1[b1.size() - 1])
         a1.pop_back();
       while (c1.size() > 0 && c1[0] < b1[0]) c1.pop_front();
       while (c1.size() > 0 && c1[c1.size() - 1] > b1[b1.size() - 1])
         c1.pop_back();
       b1.pop_front();
       b1.pop_back();
     } else {
       ans +=  c ;
       while (b1.size() > 0 && b1[0] < c1[0]) b1.pop_front();
       while (b1.size() > 0 && b1[b1.size() - 1] > c1[c1.size() - 1])
         b1.pop_back();
       while (a1.size() > 0 && a1[0] < c1[0]) a1.pop_front();
       while (a1.size() > 0 && a1[a1.size() - 1] > c1[c1.size() - 1])
         a1.pop_back();
       c1.pop_front();
       c1.pop_back();
     }
   }
   string aux = ans;
   reverse(aux.begin(), aux.end());
   if (c1.size() > 0) {
     ans +=  c ;
   } else {
     if (b1.size() > 0) {
       ans +=  b ;
     } else {
       if (a1.size()) {
         ans +=  a ;
       }
     }
   }
   ans += aux;
   if (ans.size() >= (int)n / 2)
     cout << ans;
   else
     cout <<  IMPOSSIBLE ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 T modulo(T x, T p) {
   if (x >= 0) return x % p;
   return p + x % p;
 }
 template <class T>
 T gcd(T a, T b) {
   if (b == 0) return a;
   return gcd(b, a % b);
 }
 template <class T>
 T lcm(T a, T b) {
   return a * b / gcd(a, b);
 }
 int dfs_paths(
     int u, int prev, vector<vector<pair<long long, long long> > > &G,
     long long &cur,
     priority_queue<pair<long long, pair<long long, long long> > > &pqw) {
   long long paths, total = 0, profit;
   for (pair<long long, long long> e : G[u]) {
     if (e.second == prev) continue;
     paths = dfs_paths(e.second, u, G, cur, pqw);
     profit = e.first * paths - (e.first / 2) * paths;
     pqw.push({profit, {e.first, paths}});
     cur += e.first * paths;
     total += paths;
   }
   return max(1ll, total);
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   int t;
   cin >> t;
   while (t--) {
     long long n, S;
     cin >> n >> S;
     vector<vector<pair<long long, long long> > > G(n);
     int u, v;
     long long w;
     for (int i = 0; i < n - 1; i++) {
       cin >> u >> v >> w;
       u--;
       v--;
       G[u].push_back({w, v});
       G[v].push_back({w, u});
     }
     long long cur = 0;
     priority_queue<pair<long long, pair<long long, long long> > > pqw;
     dfs_paths(0, -1, G, cur, pqw);
     long long moves = 0, mult;
     pair<long long, pair<long long, long long> > e;
     while (cur > S) {
       e = pqw.top();
       pqw.pop();
       w = e.second.first / 2;
       mult = e.second.second;
       cur -= e.first;
       pqw.push({w * mult - (w / 2) * mult, {w, mult}});
       moves++;
     }
     cout << moves << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int long long l1, l2, r1, r2, k, ans, l, r;
   cin >> l1;
   cin >> r1;
   cin >> l2;
   cin >> r2;
   cin >> k;
   l = max(l1, l2);
   r = min(r1, r2);
   if (r - l < 0) {
     cout << 0 << endl;
     return 0;
   }
   ans = r - l + 1;
   if (k >= l && k <= r) ans--;
   cout << ans << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 clock_t __stt;
 inline void TStart() { __stt = clock(); }
 inline void TReport() {
   cerr << fixed << setprecision(3) <<
Current Case Taken Time :
        << (double)(clock() - __stt) / CLOCKS_PER_SEC <<  sec
 ;
 }
 template <typename T>
 T MIN(T a, T b) {
   return a < b ? a : b;
 }
 template <typename T>
 T MAX(T a, T b) {
   return a > b ? a : b;
 }
 template <typename T>
 T ABS(T a) {
   return a > 0 ? a : (-a);
 }
 template <typename T>
 void UMIN(T &a, T b) {
   if (b < a) a = b;
 }
 template <typename T>
 void UMAX(T &a, T b) {
   if (b > a) a = b;
 }
 int n, m, p[100005], s[100005], b[100005], inc[100005], pref[100005];
 struct ele {
   int A, B, C, id;
   void set(int _A, int _B, int _C, int _id) {
     A = _A;
     B = _B;
     C = _C;
     id = _id;
   }
 } es[100005], et[100005];
 struct qry {
   int Al, Ar, B, C;
   void set(int _Al, int _Ar, int _B, int _C) {
     Al = _Al;
     Ar = _Ar;
     B = _B;
     C = _C;
   }
 } qs[100005], qt[100005];
 namespace BIT {
 int arr[300005];
 void add(int p, int v = 1) {
   ++p;
   while (p <= 300002) {
     arr[p] += v;
     p += (p & (-p));
   }
 }
 void clr(int p) {
   ++p;
   while (p <= 300002) {
     arr[p] = 0;
     p += (p & (-p));
   }
 }
 int sum(int p) {
   int r = 0;
   ++p;
   while (p) {
     r += arr[p];
     p -= (p & (-p));
   }
   return r;
 }
 }  // namespace BIT
 bool cmpB(ele a, ele b) { return a.B < b.B; }
 bool cmpC(ele a, ele b) { return a.C < b.C; }
 bool cmpBq(qry a, qry b) { return a.B < b.B; }
 bool cmpCq(qry a, qry b) { return a.C < b.C; }
 vector<int> vs;
 int id(int v) { return lower_bound(vs.begin(), vs.end(), v) - vs.begin(); }
 int res[100005];
 void solve2d(ele *E, int ec, qry *Q, int qc) {
   int i, j, k;
   sort(E, E + ec, cmpB);
   sort(Q, Q + qc, cmpBq);
   reverse(Q, Q + qc);
   reverse(E, E + ec);
   j = 0;
   for (i = 0; i < ec; ++i) {
     int cB = E[i].B;
     while (j < qc && Q[j].B >= cB) {
       BIT::add(Q[j].Al, 1);
       BIT::add(Q[j].Ar + 1, -1);
       ++j;
     }
     res[E[i].id] += BIT::sum(E[i].A);
   }
   for (i = 0; i < qc; ++i) {
     BIT::clr(Q[i].Al);
     BIT::clr(Q[i].Ar + 1);
   }
 }
 void solve(int cl, int cr, int ql, int qr, int el, int er) {
   if (ql > qr || el > er) return;
   int i, j, k;
   if (cl == cr) {
     solve2d(es + el, er - el + 1, qs + ql, qr - ql + 1);
     return;
   }
   memcpy(et + el, es + el, sizeof(ele) * (er - el + 1));
   memcpy(qt + ql, qs + ql, sizeof(qry) * (qr - ql + 1));
   int cM = ((long long)cl + (long long)cr) / 2ll, qM, eM;
   for (qM = ql; qM <= qr; ++qM) {
     if (qt[qM].C <= cM && (qM == qr || qt[qM + 1].C > cM)) break;
   }
   if (qt[ql].C > cM) qM = ql - 1;
   for (eM = el; eM <= er; ++eM) {
     if (et[eM].C <= cM && (eM == er || et[eM + 1].C > cM)) break;
   }
   if (et[el].C > cM) eM = el - 1;
   if (er > eM && qM >= ql) solve2d(et + eM + 1, er - eM, qt + ql, qM - ql + 1);
   solve(cl, cM, ql, qM, el, eM);
   solve(cM + 1, cr, qM + 1, qr, eM + 1, er);
 }
 int main() {
   int i, j, k;
   scanf( %d%d , &n, &m);
   for (i = 0; i < n; ++i) scanf( %d , p + i);
   for (i = 0; i < n; ++i) scanf( %d , s + i);
   for (i = 0; i < n; ++i) scanf( %d , b + i);
   for (i = 0; i < m; ++i) scanf( %d , inc + i);
   for (i = 0; i < m; ++i) scanf( %d , pref + i);
   for (i = 0; i < n; ++i) {
     qs[i].Al = p[i];
     qs[i].Ar = s[i];
     vs.push_back(p[i]);
     vs.push_back(s[i]);
     qs[i].B = b[i] - p[i];
     qs[i].C = b[i] + p[i];
   }
   for (j = 0; j < m; ++j) {
     es[j].A = inc[j];
     vs.push_back(inc[j]);
     es[j].B = pref[j] - inc[j];
     es[j].C = pref[j] + inc[j];
     es[j].id = j;
   }
   sort(vs.begin(), vs.end());
   vs.erase(unique(vs.begin(), vs.end()), vs.end());
   for (i = 0; i < n; ++i) {
     qs[i].Al = id(qs[i].Al);
     qs[i].Ar = id(qs[i].Ar);
   }
   for (j = 0; j < m; ++j) {
     es[j].A = id(es[j].A);
   }
   sort(es, es + m, cmpC);
   sort(qs, qs + n, cmpCq);
   solve(0, (int)2e9 + 5, 0, n - 1, 0, m - 1);
   for (i = 0; i < m; ++i) {
     printf( %d  , res[i]);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   scanf( %d , &t);
   while (t--) {
     long long x1, y1, x2, y2;
     scanf( %lld%lld%lld%lld , &x1, &y1, &x2, &y2);
     long long lenx = abs(x1 - x2);
     long long leny = abs(y1 - y2);
     long long ans = lenx + leny;
     if (lenx && leny) ans += 2;
     printf( %lld
 , ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long int n;
   cin >> n;
   cout << n % 2;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n;
   while (cin >> n) {
     vector<long long> vec;
     for (long long i = 2; i * i <= n; i++) {
       while (n % i == 0) {
         vec.push_back(i);
         n /= i;
       }
     }
     if (n > 1) {
       vec.push_back(n);
     }
     if (vec.size() < 2) {
       printf( 1
0
 );
     } else if (vec.size() == 2) {
       printf( 2
 );
     } else {
       printf( 1
%lld
 , vec[0] * vec[1]);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, num, cnt;
 int dat[1009][1009], ans[1000009];
 bool vis[1009][1009];
 int dx[4] = {-1, 0, 1, 0};
 int dy[4] = {0, 1, 0, -1};
 int wall[4] = {8, 4, 2, 1};
 void dfs(int x, int y) {
   if (vis[x][y] || x < 0 || x > n || y < 0 || y > m) return;
   vis[x][y] = 1;
   num++;
   for (int i = 0; i < 4; i++) {
     if ((dat[x][y] & wall[i]) ||
         (dat[x + dx[i]][y + dy[i]] & wall[(i + 2) % 4]))
       continue;
     dfs(x + dx[i], y + dy[i]);
   }
 }
 int main() {
   cin >> n >> m;
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= m; j++) {
       cin >> dat[i][j];
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= m; j++) {
       if (!vis[i][j]) {
         num = 0;
         dfs(i, j);
         ans[++cnt] = num;
       }
     }
   }
   sort(ans + 1, ans + 1 + cnt);
   for (int i = cnt; i > 0; i--) cout << ans[i] <<    ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, m, ind, sum = 0;
   cin >> n;
   long long mas[n];
   for (long long i = 0; i < n; i++) {
     cin >> mas[i];
     sum += mas[i];
   }
   cin >> m;
   long long arr[m];
   sort(mas, mas + n);
   for (long long i = 0; i < m; i++) {
     cin >> arr[i];
     cout << sum - mas[n - arr[i]] << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, nxt[200010], a[200010], sz[200010][31];
 long long ans = 0;
 map<int, int> mp;
 inline char gc() {
   static char *S, *T, buf[1 << 16];
   if (T == S) {
     T = (S = buf) + fread(buf, 1, 1 << 16, stdin);
     if (T == S) return EOF;
   }
   return *S++;
 }
 inline int read() {
   int x = 0, f = 1;
   char ch = gc();
   while (ch < '0' || ch > '9') {
     if (ch == '-') f = -1;
     ch = gc();
   }
   while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = gc();
   return x * f;
 }
 inline int check(int l, int r) {
   int res = 0;
   for (int i = 0; i <= 30; ++i) res |= ((sz[r][i] - sz[l - 1][i]) > 0) << i;
   return res;
 }
 int main() {
   n = read();
   for (int i = 1; i <= n; ++i) {
     a[i] = read(), nxt[i] = mp[a[i]], mp[a[i]] = i;
     for (int j = 0; j <= 30; ++j) {
       sz[i][j] = sz[i - 1][j];
       if (a[i] & (1 << j)) sz[i][j]++;
     }
   }
   for (int i = 1; i <= n; ++i) {
     int l = max(nxt[i] + 1, 1), r = i, nowl = i;
     while (l <= r) {
       int mid = l + r >> 1;
       if (check(mid, i) == a[i])
         nowl = mid, r = mid - 1;
       else
         l = mid + 1;
     }
     l = i, r = n;
     int nowr = i;
     while (l <= r) {
       int mid = l + r >> 1;
       if (check(i, mid) == a[i])
         nowr = mid, l = mid + 1;
       else
         r = mid - 1;
     }
     ans += 1ll * (i - nowl + 1) * (nowr - i + 1);
   }
   printf( extra
 , 1ll * n * (n + 1) / 2 - ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, t, twoc, onec, opt, old;
 int main() {
   cin >> n;
   cin >> t;
   if (t == 1)
     onec++;
   else
     twoc++;
   old = t;
   for (int i = 1; i < n; i++) {
     cin >> t;
     if (t == 1) {
       if (old == 2)
         onec = 1;
       else
         onec++;
     } else {
       if (old == 1)
         twoc = 1;
       else
         twoc++;
     }
     opt = max(opt, min(onec, twoc));
     old = t;
   }
   cout << opt * 2;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int ar[1000001];
 int main() {
   int n, k;
   cin >> n >> k;
   if (k == n) {
     cout << -1 << endl;
     return 0;
   }
   if (k == n - 1) {
     for (int i = 0; i < n; i++) cout << i + 1 <<    ;
     cout << endl;
     return 0;
   }
   for (int i = 2; i <= k + 1; i++) ar[i] = i;
   ar[1] = k + 2;
   ar[n] = 1;
   for (int i = k + 2; i < n; i++) ar[i] = i + 1;
   for (int i = 1; i <= n; i++) cout << ar[i] <<    ;
   cout << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int pow(int a, int b, int c) {
   int ret = 1;
   for (; b; b >>= 1, a = 1LL * a * a % c)
     if (b & 1) ret = 1LL * ret * a % c;
   return ret;
 }
 const int N = 2e3 + 10, M = 4.05e6 + 10;
 int h[N], g[N], p[M], n1[M], ee, low[N], dfn[N], dfntot, vis[N], n, m, st[N],
     top, id[N], tot, sz[N], ans, dp[N], ok[N];
 pair<int, int> e[M];
 void ae(int *A, int x, int y) {
   p[ee] = y;
   n1[ee] = A[x];
   A[x] = ee++;
 }
 void tarjan(int u, int fa) {
   vis[u] = 1;
   low[u] = dfn[u] = ++dfntot;
   st[++top] = u;
   for (int i = h[u]; ~i; i = n1[i])
     if (p[i] != fa) {
       if (!vis[p[i]])
         tarjan(p[i], u), low[u] = min(low[u], low[p[i]]);
       else
         low[u] = min(low[u], dfn[p[i]]);
       if (low[p[i]] > dfn[u]) {
         ++tot;
         while (st[top] != p[i]) id[st[top--]] = tot, sz[tot]++;
         id[st[top--]] = tot;
         sz[tot]++;
       }
     }
 }
 void dfs(int u, int fa) {
   dp[u] = sz[u];
   for (int i = g[u]; ~i; i = n1[i])
     if (p[i] != fa) {
       dfs(p[i], u);
       dp[u] += dp[p[i]];
     }
 }
 int main() {
   scanf( %d%d , &n, &m);
   memset(h, -1, sizeof(h));
   memset(g, -1, sizeof(g));
   int x, y;
   for (int i = 1; i <= m; i++) scanf( %d%d , &x, &y), ae(h, x, y), ae(h, y, x);
   for (int i = 1; i <= n; i++)
     if (!vis[i]) tarjan(i, 0);
   ++tot;
   while (top) id[st[top--]] = tot, sz[tot]++;
   for (int i = 1; i <= n; i++)
     for (int j = h[i]; ~j; j = n1[j])
       if (id[i] != id[p[j]]) ae(g, id[i], id[p[j]]);
   for (int i = 1; i <= tot; i++) ans += sz[i] * (sz[i] - 1);
   int ret = 0;
   for (int i = 1; i <= tot; i++) {
     int tmp = 0;
     dfs(i, 0);
     for (int j = 1; j <= tot; j++) tmp += (dp[j] - sz[j]) * sz[j];
     for (int j = 0; j <= n; j++) ok[j] = 0;
     ok[0] = 1;
     for (int k = g[i]; ~k; k = n1[k])
       for (int j = n; j >= dp[p[k]]; j--) ok[j] |= ok[j - dp[p[k]]];
     int mx = 0;
     for (int j = 0; j <= n; j++)
       if (ok[j]) mx = max(mx, j * (n - sz[i] - j));
     ret = max(ret, tmp + mx);
   }
   printf( %d
 , n + ans + ret);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int m = 1000000007;
 long long int n1, n2 = 0, sum = 0;
 long long int gcd(long long int a, long long int b) {
   return (b == 0) ? a : gcd(b, a % b);
 }
 bool compare(pair<string, long long int> p1, pair<string, long long int> p2) {
   if (p1.second != p2.second) {
     return p1.second > p2.second;
   } else
     return p1.first < p2.first;
 }
 signed main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   long long int t = 1;
   while (t--) {
     long long int n;
     cin >> n;
     bool prime[n + 2];
     memset(prime, true, sizeof(prime));
     for (long long int p = 2; p * p <= n + 1; p++) {
       if (prime[p] == true) {
         for (long long int i = p * p; i <= n + 1; i += p) prime[i] = false;
       }
     }
     if (n > 2)
       cout << 2 <<
 ;
     else
       cout << 1 <<
 ;
     for (long long int i = 2; i < n + 2; i++) {
       if (prime[i])
         cout << 1 << ' ';
       else
         cout << 2 << ' ';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 mt19937_64 mt(time(0));
 set<pair<long long, long long> > dp[49];
 void insert(set<pair<long long, long long> >& table, long long a, long long b) {
   if (table.empty()) {
     table.insert({a, b});
     return;
   }
   auto it = table.lower_bound({b, LLONG_MAX});
   while (it != table.begin()) {
     it--;
     if (it->second < a) break;
     a = min(it->first, a);
     b = max(it->second, b);
     if (it == table.begin()) {
       table.erase(it);
       break;
     } else {
       table.erase(it++);
     }
   }
   table.insert({a, b});
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cout.precision(10);
   cout << fixed;
   long long n;
   cin >> n;
   for (int i = 0; i <= min(48LL, n); i++) {
     for (int j = 0; j <= 8 && i + j <= n; j++) {
       int tmp = i * 9 + j * 4;
       long long a = tmp / 49;
       long long b = n - i - j + tmp / 49;
       insert(dp[tmp % 49], a, b);
     }
   }
   long long ret = 0;
   for (int i = 0; i < 49; i++) {
     for (auto item : dp[i]) {
       ret += item.second - item.first + 1;
     }
   }
   cout << ret << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using namespace std;
 struct node {
   int l, m, r;
 };
 node t[3000005];
 int arr[300005], n, m;
 long long a[300005];
 int sign(long long x) {
   if (x > 0) return 1;
   if (x < 0) return -1;
   return 0;
 }
 void cal(int x, int l, int r) {
   int m = (l + r) / 2;
   t[x].m = max(t[x * 2].m, t[x * 2 + 1].m);
   t[x].l = t[x * 2].l;
   t[x].r = t[x * 2 + 1].r;
   if (!!a[m] && !!a[m + 1] && sign(a[m]) >= sign(a[m + 1])) {
     t[x].m = max(t[x].m, t[x * 2].r + t[x * 2 + 1].l);
     if (t[2 * x].m == m - l + 1) t[x].l = t[2 * x].l + t[2 * x + 1].l;
     if (t[2 * x + 1].m == r - m) t[x].r = t[2 * x].r + t[2 * x + 1].r;
   }
 }
 void build(int x, int l, int r) {
   if (l == r) {
     int tmp = !!a[l];
     t[x] = {tmp, tmp, tmp};
     return;
   }
   int m = (l + r) / 2;
   build(x * 2, l, m);
   build(x * 2 + 1, m + 1, r);
   cal(x, l, r);
 }
 void update(int x, int l, int r, int pos, int d) {
   if (l == r) {
     a[pos] += d;
     int tmp = !!a[pos];
     t[x] = {tmp, tmp, tmp};
     return;
   }
   int m = (l + r) / 2;
   if (pos <= m)
     update(x * 2, l, m, pos, d);
   else
     update(x * 2 + 1, m + 1, r, pos, d);
   cal(x, l, r);
 }
 int main() {
   cin >> n;
   for (int i = 0; i < n; i++) scanf( %d , &arr[i]);
   for (int i = 0; i + 1 < n; i++) a[i] = arr[i + 1] - arr[i];
   if (n > 1) build(1, 0, n - 2);
   cin >> m;
   while (m--) {
     int l, r, d;
     scanf( %d%d%d , &l, &r, &d);
     if (n == 1) {
       printf( %d
 , 1);
       continue;
     }
     if (l > 1) update(1, 0, n - 2, l - 2, d);
     if (r < n) update(1, 0, n - 2, r - 1, -d);
     printf( %d
 , t[1].m + 1);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = int(1e9);
 const int MOD = INF + 7;
 const long long INFll = 1ll * INF * INF;
 const long double ldINF = 1e+018;
 const long double EPS = 0.000000001;
 const int MAXN = 150;
 const int N = 100001;
 const int seg[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
 struct feature {
   int b, i, r;
 };
 bool comp(feature x, feature y) {
   return x.r > y.r || (x.r == y.r && x.b < y.b);
 }
 int t[1001000];
 int cur = 0;
 int query(int l, int r) {
   int res = 0;
   for (l += cur, r += cur; l < r; l >>= 1, r >>= 1) {
     if (l & 1) res = max(res, t[l++]);
     if (r & 1) res = max(res, t[--r]);
   }
   return res;
 }
 void modify(int p, int value) {
   t[p += cur] = value;
   while (p > 1) {
     p >>= 1;
     t[p] = max(t[p << 1], t[p << 1 | 1]);
   }
 }
 int main() {
   ios_base::sync_with_stdio(0);
   int n;
   cin >> n;
   vector<feature> v(n);
   for (int i = 0; i < n; i++) cin >> v[i].r;
   for (int i = 0; i < n; i++) cin >> v[i].b;
   for (int i = 0; i < n; i++) cin >> v[i].i;
   vector<int> b;
   for (int i = 0; i < n; i++) b.push_back(v[i].b);
   sort(b.begin(), b.end());
   map<int, int> m;
   m[b[0]] = 0;
   for (int i = 1; i < n; i++)
     if (b[i] != b[i - 1]) m[b[i]] = ++cur;
   cur++;
   for (int i = 0; i < n; i++) v[i].b = m[v[i].b];
   int ans = 0;
   sort(v.begin(), v.end(), comp);
   for (int i = 0; i < n; i++) {
     int d = query(v[i].b + 1, cur);
     if (d > v[i].i) {
       ans++;
     }
     modify(v[i].b, max(t[v[i].b + cur], v[i].i));
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   long long int t = 1;
   while (t--) {
     long long int n, d = 0, r = 0;
     cin >> n;
     string s;
     cin >> s;
     char x;
     for (long long int i = 0; i < s.length(); i++) {
       if (s[i] == 'R') {
         if (d) {
           d--;
         } else {
           s += 'R';
           r++;
         }
       } else {
         if (r) {
           r--;
         } else {
           s += 'D';
           d++;
         }
       }
       x = s[i];
       if (d == n || r == n) {
         break;
       }
     }
     cout << x << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 #define fasterio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 #define X first
 #define Y second
 #define forn(i, n) for(int i=0;i<(n);i++)
 #define pb push_back
 using namespace std;
 typedef long long ll;
 typedef pair<int, int> pii;
 const int inf = 0x3f3f3f3f;
 const long long mod = 1e9+7;
 const long long maxn = 100010;

 int gao (int x1, int y1, int x2, int y2, int xf, int yf) {
 	auto abs = [&](int a, int b){return a>b?a-b:b-a;};
 	int ans = abs(x1, x2) + abs(y1, y2);

 	if (x1 == x2 && y1 == y2) {
 		return ans;
 	} else if (x1 == x2 && x2 == xf) { // on the same x axis
 		if ((y1<yf && yf<y2) || (y2<yf && yf<y1))
 			ans += 2;
 	} else if (y1 == y2 && y2 == yf) {
 		if ((x1<xf && xf<x2)||(x2<xf && xf<x1))
 			ans += 2;
 	}
 	return ans;
 }
 int main() {
  int t;
  cin>>t;
  	while(t--) {
 		int xa, ya, xb, yb, xf, yf;
 		cin>>xa>>ya>>xb>>yb>>xf>>yf;

 		if(xa==xb && ya==yb) {
 			cout<<0<<endl;
 			continue;
 		}

 		int ans = gao(xa, ya, xb, yb, xf, yf);
 		cout<<ans<<endl;
 	}
 }


#include <bits/stdc++.h>
 using namespace std;
 int l_f, l_h;
 long long int dp[101][101][11][11];
 int getans(int f, int h, int k1, int k2) {
   int x = 0, y = 0;
   if ((f + h) == 0) return 1;
   if (dp[f][h][k1][k2] != -1) return dp[f][h][k1][k2];
   if (f > 0 && k1 > 0) x = getans(f - 1, h, k1 - 1, l_h);
   if (h > 0 && k2 > 0) y = getans(f, h - 1, l_f, k2 - 1);
   return dp[f][h][k1][k2] = (x + y) % 100000000;
 }
 int main() {
   int t = 1;
   while (t--) {
     int n1, n2;
     cin >> n1 >> n2 >> l_f >> l_h;
     memset(dp, -1, sizeof(dp));
     int ans = getans(n1, n2, l_f, l_h);
     cout << ans << '
';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n;
   cin >> n;
   map<string, string> mp;
   map<string, string>::iterator it;
   for (int i = 0; i < n; ++i) {
     string x, y;
     cin >> x >> y;
     it = mp.begin();
     bool found = false;
     for (; it != mp.end(); ++it) {
       if (it->second == x) {
         it->second = y;
         found = true;
       }
     }
     if (!found) mp[x] = y;
   }
   cout << mp.size() << endl;
   it = mp.begin();
   for (; it != mp.end(); it++) cout << it->first <<     << it->second << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 pair<int, int> h[100010], h1[100010];
 long long cnt[100010], pre[100010], sum[100010];
 int num[100010], mx[100010], n, tt, t1;
 inline int rd() {
   int x = 0;
   char ch = getchar();
   for (; ch < '0' || ch > '9'; ch = getchar())
     ;
   for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
   return x;
 }
 inline int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); }
 inline long long calc(long long l, long long r) {
   return (l + r) * (r - l + 1) / 2;
 }
 inline long long anol_gcd(long long n, long long a, long long b, long long c) {
   if (n < 0) return 0;
   if (!a) return (b / c) * (n + 1);
   if (a >= c || b >= c)
     return (a / c) * n * (n + 1) / 2 + (b / c) * (n + 1) +
            anol_gcd(n, a % c, b % c, c);
   long long hh = (a * n + b) / c;
   return n * hh - anol_gcd(hh - 1, c, c - b - 1, a);
 }
 inline long long gao(int x, int y, long long mid) {
   if (!cnt[x] || !cnt[y]) return 0;
   mid -= pre[y - 1] - pre[x];
   if (mid < 0) return 0;
   long long mn = (mid - cnt[y] * y) / x;
   mn = max(mn, 0LL);
   if (mn >= cnt[x]) return cnt[x] * cnt[y];
   long long res = mn * cnt[y];
   mn++;
   long long mx = mid / x;
   mx = min(mx, cnt[x]);
   if (mn <= mx) res += anol_gcd(mx - mn, x, mid - mx * x, y);
   return res;
 }
 inline long long check(long long mid) {
   long long res = 0;
   int now = 1;
   for (int i = 1; i <= 100000; i++) {
     now = max(now, i);
     long long hh = min(mid / i, cnt[i]);
     if (cnt[i]) res += calc(cnt[i] - hh + 1, cnt[i]);
     while (now + 1 <= 100000 && pre[now + 1] - pre[i - 1] <= mid) now++;
     res += (sum[now] - sum[i]) * cnt[i];
     mx[i] = now;
   }
   for (int i = 1; i <= 100000; i++)
     for (int j = mx[i] + 1; j <= min(mx[i + 1] + 1, 100000); j++)
       res += gao(i, j, mid);
   return res;
 }
 int main() {
   n = rd();
   for (int i = 1; i <= n; i++) num[i] = rd();
   cnt[num[1]]++;
   h[tt = 1] = pair<int, int>(1, num[1]);
   for (int i = 2; i <= n; i++) {
     t1 = tt;
     for (int j = 1; j <= t1; j++)
       h1[j] = h[j], h1[j].second = gcd(h1[j].second, num[i]);
     h1[++t1] = pair<int, int>(1, num[i]);
     tt = 0;
     for (int j = 1; j <= t1;) {
       int k = j, now = 0;
       for (; k <= t1 && h1[k].second == h1[j].second; k++) now += h1[k].first;
       h[++tt] = pair<int, int>(now, h1[j].second);
       j = k;
     }
     for (int j = 1; j <= tt; j++) cnt[h[j].second] += h[j].first;
   }
   for (int i = 1; i <= 100000; i++)
     pre[i] = pre[i - 1] + cnt[i] * i, sum[i] = sum[i - 1] + cnt[i];
   long long all = (long long)n * (n + 1) / 2;
   all = all * (all + 1) / 2;
   all = (all + 1) / 2;
   long long l = 0, r = (long long)n * (n + 1) / 2 * 100000;
   while (l < r) {
     long long mid = (l + r) >> 1;
     if (check(mid) >= all)
       r = mid;
     else
       l = mid + 1;
   }
   printf( %lld
 , l);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, k, a[101], m;
 int main() {
   m = 101;
   cin >> n >> k;
   for (int i = 1; i <= n; i++) {
     cin >> a[i];
     if (k % a[i] == 0) m = min(m, k / a[i]);
   }
   cout << m;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int cnt[50];
 int main() {
   int n, k;
   cin >> n >> k;
   int aa;
   for (int i = 0; i < n; i++) {
     int tmp = 0;
     for (int j = k - 1; j >= 0; j--) {
       scanf( %d , &aa);
       tmp += (aa << j);
     }
     cnt[tmp]++;
   }
   if (cnt[0]) {
     cout <<  YES ;
     return 0;
   }
   int tot = (1 << k) - 1;
   for (int i = 1; i <= tot; i++) {
     if (cnt[i]) {
       for (int j = 1; j <= tot; j++) {
         if (cnt[j] && (i & j) == 0) {
           cout <<  YES ;
           return 0;
         }
       }
     }
   }
   cout <<  NO ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline void read(int &x) {
   x = 0;
   int f = 1;
   char c = getchar();
   while (c > '9' || c < '0') {
     if (c == '-') f = -1;
     c = getchar();
   }
   while (c >= '0' && c <= '9')
     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   x *= f;
 }
 const int maxn = 100100;
 struct node {
   int v, nxt;
 } e[maxn];
 int head[maxn], tot;
 inline void add(int x, int y) {
   tot++;
   e[tot].v = y;
   e[tot].nxt = head[x];
   head[x] = tot;
 }
 struct AA {
   int s, num;
 };
 map<string, int> h;
 vector<AA> ask[maxn];
 set<int> hip[maxn];
 int n, q, s, t, cnt, name[maxn], dep[maxn], son[maxn], size[maxn], ans[maxn];
 bool vis[maxn];
 char ch[30];
 inline void dfs1(int x, int fa) {
   size[x] = 1;
   dep[x] = dep[fa] + 1;
   for (int i = head[x]; i; i = e[i].nxt) {
     int v = e[i].v;
     if (v == fa) continue;
     dfs1(v, x);
     size[x] += size[v];
     if (son[x] == 0 || size[v] > size[son[x]]) son[x] = v;
   }
 }
 inline void calc(int x, int fa) {
   hip[dep[x]].insert(name[x]);
   for (int i = head[x]; i; i = e[i].nxt) {
     int v = e[i].v;
     if (v == fa || vis[v]) continue;
     calc(v, x);
   }
 }
 inline void dfs2(int x, int fa, int keep) {
   for (int i = head[x]; i; i = e[i].nxt) {
     int v = e[i].v;
     if (v == fa || v == son[x]) continue;
     dfs2(v, x, 0);
   }
   if (son[x]) dfs2(son[x], x, 1), vis[son[x]] = true;
   calc(x, fa);
   vis[son[x]] = false;
   for (int i = 0; i < ask[x].size(); i++) {
     if (dep[x] + ask[x][i].s <= n)
       ans[ask[x][i].num] = hip[dep[x] + ask[x][i].s].size();
   }
   if (!keep) {
     for (int i = dep[x]; !hip[i].empty(); i++) {
       hip[i].clear();
     }
   }
 }
 int main() {
   read(n);
   for (int i = 1; i <= n; i++) {
     scanf( %s , ch);
     if (h.find(ch) == h.end()) h[ch] = ++cnt;
     name[i] = h[ch];
     read(s), add(s, i);
   }
   read(q);
   AA tmp;
   for (int i = 1; i <= q; i++) {
     read(s), read(t);
     tmp.s = t;
     tmp.num = i;
     ask[s].push_back(tmp);
   }
   dep[0] = -1;
   dfs1(0, 0);
   dfs2(0, 0, 1);
   for (int i = 1; i <= q; i++) printf( %d
 , ans[i]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> v[250005];
 int visited[250005];
 int n, m, x, y;
 void zero() {
   for (int i = 0; i <= n + 2; i++) {
     visited[i] = 0;
     v[i].clear();
   }
 }
 void bipartite(int u) {
   if (visited[u] == 1) {
     x++;
   } else if (visited[u] == 2) {
     y++;
   }
   for (int i = 0; i < v[u].size(); i++) {
     int a = v[u][i];
     if (visited[a] == 0 && visited[u] == 1) {
       visited[a] = 2;
       bipartite(a);
     } else if (visited[a] == 0 && visited[u] == 2) {
       visited[a] = 1;
       bipartite(a);
     }
   }
 }
 int main() {
   int i, a, b, t;
   scanf( %d , &t);
   while (t--) {
     scanf( %d %d , &n, &m);
     zero();
     for (i = 1; i <= m; i++) {
       scanf( %d %d , &a, &b);
       v[a].push_back(b);
       v[b].push_back(a);
     }
     visited[1] = 1;
     x = 0;
     y = 0;
     bipartite(1);
     printf( %d
 , min(x, y));
     if (x <= y) {
       for (i = 1; i <= n; i++) {
         if (visited[i] == 1) {
           printf( %d  , i);
         }
       }
     } else {
       for (i = 1; i <= n; i++) {
         if (visited[i] == 2) {
           printf( %d  , i);
         }
       }
     }
     printf(
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize( O3 )
 #pragma GCC optimize( unroll-loops )
 using namespace std;
 using ll = long long;
 using db = long double;
 using ii = pair<int, int>;
 const int N = 1e7 + 5, LG = 19, MOD = 1e9 + 7;
 const int SQ = 225;
 const long double EPS = 1e-7;
 int fast(int b, int e) {
   int res = 1;
   for (; e; e >>= 1, b = 1ll * b * b % MOD)
     if (e & 1) res = 1ll * res * b % MOD;
   return res;
 }
 vector<int> adj[10005];
 int n, k;
 map<int, map<int, ll>> mat;
 int32_t main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cin >> n >> k;
   vector<int> deg(n + 1);
   for (int i = 1; i <= k; i++)
     for (int j = i + 1; j <= k; j++) {
       adj[j].push_back(i);
       deg[j]++;
       deg[i]++;
     }
   for (int i = k + 1; i <= n; i++) {
     for (int j = 0; j < k; j++) {
       int x;
       cin >> x;
       adj[i].push_back(x);
       deg[i]++;
       deg[x]++;
     }
   }
   for (int i = 1; i <= n; i++) {
     mat[i][i] = deg[i];
     for (auto x : adj[i]) {
       mat[i][x] = MOD - 1;
       mat[x][i] = MOD - 1;
     }
   }
   ll ans = 1;
   for (int i = n; i >= 2; --i) {
     ll v = mat[i][i];
     ll inv = fast(MOD - v, MOD - 2);
     ans = 1ll * ans * v % MOD;
     for (auto j : adj[i]) {
       ll coff = inv * mat[j][i] % MOD;
       for (auto k : adj[i]) {
         mat[j][k] = (mat[j][k] + mat[i][k] * coff) % MOD;
       }
     }
   }
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long mod = 1000000007;
 long long powermodm(long long x, long long n, long long M) {
   long long result = 1;
   while (n > 0) {
     if (n % 2 == 1) result = (result * x) % M;
     x = (x * x) % M;
     n = n / 2;
   }
   return result;
 }
 long long power(long long _a, long long _b) {
   long long _r = 1;
   while (_b) {
     if (_b % 2 == 1) _r = (_r * _a);
     _b /= 2;
     _a = (_a * _a);
   }
   return _r;
 }
 long long gcd(long long a, long long b) {
   if (a == 0) return b;
   return gcd(b % a, a);
 }
 long long lcm(long long a, long long b) {
   return (max(a, b) / gcd(a, b)) * min(a, b);
 }
 long long modInverse(long long x) { return powermodm(x, mod - 2, mod); }
 long long fact[100005];
 long long ncrmodm(long long n, long long r, long long m) {
   return (fact[n] * (modInverse(fact[n - r]) * modInverse(fact[r])) % m) % m;
 }
 void init_fact() {
   fact[0] = 0;
   for (long long i = 1; i < 100001; i++) fact[i] = fact[i - 1] * i;
 }
 void write(long long a[], long long n) {
   for (long long i = 0; i < n; i++) cout << a[i] <<    ;
   cout << endl;
 }
 void show(vector<long long> v) {
   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ;
   cout << endl;
 }
 vector<vector<long long> > vec;
 vector<long long> visited;
 void dfs(int x, int p = -1) {
   visited[x] = 1;
   for (int i = 0; i < vec[x].size(); i++) {
     if (p == vec[x][i]) continue;
     if (!visited[vec[x][i]]) {
       visited[vec[x][i]] = 1;
       dfs(vec[x][i], x);
     }
   }
 }
 bool prime[100000];
 void seive() {
   int n = 100000;
   memset(prime, true, sizeof(prime));
   for (int p = 2; p * p <= n; p++) {
     if (prime[p] == true) {
       for (int i = p * p; i <= n; i += p) prime[i] = false;
     }
   }
 }
 void solve() {
   long long n, k;
   cin >> n >> k;
   long long a[n];
   long long ma = -INT_MAX, mi = INT_MAX;
   for (long long i = 0; i < n; i++) {
     cin >> a[i];
     ma = max(ma, a[i]);
     mi = min(mi, a[i]);
   }
   if (k % 2) {
     for (long long i = 0; i < n; i++) {
       cout << ma - a[i] <<    ;
     }
     cout << endl;
   } else {
     for (long long i = 0; i < n; i++) {
       cout << -mi + a[i] <<    ;
     }
     cout << endl;
   }
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   int t = 1;
   cin >> t;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void extgcd(long long a, long long b, long long& x, long long& y) {
   if (b == 0) {
     assert(a == 1);
     x = 1, y = 0;
     return;
   }
   extgcd(b, a % b, y, x);
   y -= a / b * x;
 }
 int inv(int a, int c) {
   long long x, y;
   extgcd(a, c, x, y);
   return (x % c + c) % c;
 }
 long long modpow(long long a, int b, int c) {
   long long res = 1;
   for (; b; b >>= 1) {
     if (b & 1) res = res * a % c;
     a = a * a % c;
   }
   return res;
 }
 const int MAXN = 100005, MAXT = 262150;
 int num[10], tot, n, m, Q, tn;
 struct Value {
   int cnt[10];
   long long rest;
   void clear() {
     memset(cnt, 0, sizeof(cnt));
     rest = 1;
   }
   Value() { clear(); }
   Value& operator*=(int x) {
     for (int i = 0; i < tot; i++)
       while (x % num[i] == 0) x /= num[i], ++cnt[i];
     rest = rest * x % m;
     return *this;
   }
   Value& operator/=(int x) {
     for (int i = 0; i < tot; i++)
       while (x % num[i] == 0) x /= num[i], --cnt[i];
     rest = rest * inv(x, m) % m;
     return *this;
   }
   Value& operator*=(const Value& v) {
     for (int i = 0; i < tot; i++) cnt[i] += v.cnt[i];
     rest = rest * v.rest % m;
     return *this;
   }
   long long get_val() {
     long long res = rest;
     for (int i = 0; i < tot; i++) res = res * modpow(num[i], cnt[i], m) % m;
     return res;
   }
 } tag[MAXT];
 long long sum[MAXT];
 void build(int l = 1, int r = tn, int k = 1) {
   if (l == r) return;
   int mid = (l + r) >> 1;
   build(l, mid, k << 1);
   build(mid + 1, r, k << 1 | 1);
   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;
 }
 void pushdown(int k) {
   int flag = 1;
   for (int i = 0; i < tot; i++)
     if (tag[k].cnt[i]) {
       flag = 0;
       break;
     }
   if (flag && tag[k].rest == 1) return;
   int ls = k << 1, rs = k << 1 | 1;
   tag[ls] *= tag[k], tag[rs] *= tag[k];
   long long v = tag[k].get_val();
   (sum[ls] *= v) %= m, (sum[rs] *= v) %= m;
   tag[k].clear();
 }
 void mul(int a, int b, int x, int l = 1, int r = tn, int k = 1) {
   if (a > r || b < l) return;
   if (a <= l && b >= r) {
     tag[k] *= x, (sum[k] *= x) %= m;
     return;
   }
   pushdown(k);
   int mid = (l + r) >> 1;
   mul(a, b, x, l, mid, k << 1);
   mul(a, b, x, mid + 1, r, k << 1 | 1);
   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;
 }
 void divi(int a, int x, int l = 1, int r = tn, int k = 1) {
   if (l == r) {
     tag[k] /= x;
     sum[k] = tag[k].get_val();
     return;
   }
   int mid = (l + r) >> 1;
   pushdown(k);
   if (a <= mid)
     divi(a, x, l, mid, k << 1);
   else
     divi(a, x, mid + 1, r, k << 1 | 1);
   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m;
 }
 int query(int a, int b, int l = 1, int r = tn, int k = 1) {
   if (a > r || b < l) return 0;
   if (a <= l && b >= r) return sum[k];
   pushdown(k);
   int mid = (l + r) >> 1;
   return (query(a, b, l, mid, k << 1) + query(a, b, mid + 1, r, k << 1 | 1)) %
          m;
 }
 void pre() {
   int kk = m;
   for (int i = 2; i * i <= kk; i++)
     if (kk % i == 0) {
       while (kk % i == 0) kk /= i;
       num[tot++] = i;
     }
   if (kk > 1) num[tot++] = kk;
 }
 int main() {
   scanf( %d%d , &n, &m);
   pre();
   for (tn = 1; tn < n; tn <<= 1)
     ;
   for (int i = 1; i <= n; i++) {
     scanf( %lld , &sum[i + tn - 1]);
     tag[i + tn - 1] *= sum[i + tn - 1];
     sum[i + tn - 1] %= m;
   }
   build();
   scanf( %d , &Q);
   while (Q--) {
     int a, b, c, d;
     scanf( %d%d%d , &a, &b, &c);
     if (a == 1) {
       scanf( %d , &d);
       mul(b, c, d);
     } else if (a == 2)
       divi(b, c);
     else
       printf( %d
 , query(b, c));
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double PI = acos(-1);
 const double eps = 1e-9;
 const int inf = 2000000000;
 const long long infLL = 9000000000000000000;
 inline bool checkBit(long long n, int i) { return n & (1LL << i); }
 inline long long setBit(long long n, int i) { return n | (1LL << i); }
 inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
 int dx4[] = {0, 0, +1, -1};
 int dy4[] = {+1, -1, 0, 0};
 int dx8[] = {+1, 0, -1, 0, +1, +1, -1, -1};
 int dy8[] = {0, +1, 0, -1, +1, -1, +1, -1};
 inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
 inline bool isLeapYear(long long year) {
   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
 }
 inline bool isIntege(double num) { return (num == (int)num); }
 inline bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) {
   return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2);
 }
 inline double coDist(double x1, double y1, double x2, double y2) {
   return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
 }
 inline double TriangleAreaWithSide(double a, double b, double c) {
   double s = (a + b + c) / 2;
   double area = sqrt(s * (s - a) * (s - b) * (s - c));
   return area;
 }
 inline double area3(double x1, double y1, double x2, double y2, double x3,
                     double y3) {
   double A = abs((x1 * y2 + x2 * y3 + x3 * y1) - (y1 * x2 + y2 * x3 + y3 * x1));
   A /= 2.0;
   return A;
 }
 inline long double degreetoradian(long double x) {
   long double val = PI * x;
   val /= (180.0);
   return val;
 }
 inline void normal(long long &a) {
   a %= 1000000007;
   (a < 0) && (a += 1000000007);
 }
 template <typename T>
 inline T gcd(T a, T b) {
   T c;
   while (b) {
     c = b;
     b = a % b;
     a = c;
   }
   return a;
 }
 inline long long modMul(long long a, long long b) {
   a %= 1000000007, b %= 1000000007;
   normal(a), normal(b);
   return (a * b) % 1000000007;
 }
 inline long long modAdd(long long a, long long b) {
   a %= 1000000007, b %= 1000000007;
   normal(a), normal(b);
   return (a + b) % 1000000007;
 }
 inline long long modSub(long long a, long long b) {
   a %= 1000000007, b %= 1000000007;
   normal(a), normal(b);
   a -= b;
   normal(a);
   return a;
 }
 inline long long modPow(long long b, long long p) {
   long long r = 1;
   while (p) {
     if (p & 1) r = modMul(r, b);
     b = modMul(b, b);
     p >>= 1;
   }
   return r;
 }
 inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
 inline long long modDiv(long long a, long long b) {
   return modMul(a, modInverse(b));
 }
 inline bool equalTo(double a, double b) {
   if (fabs(a - b) <= eps)
     return true;
   else
     return false;
 }
 inline bool notEqual(double a, double b) {
   if (fabs(a - b) > eps)
     return true;
   else
     return false;
 }
 inline bool lessThan(double a, double b) {
   if (a + eps < b)
     return true;
   else
     return false;
 }
 inline bool lessThanEqual(double a, double b) {
   if (a < b + eps)
     return true;
   else
     return false;
 }
 inline bool greaterThan(double a, double b) {
   if (a > b + eps)
     return true;
   else
     return false;
 }
 inline bool greaterThanEqual(double a, double b) {
   if (a + eps > b)
     return true;
   else
     return false;
 }
 inline string to_s(int t) {
   stringstream ss;
   ss << t;
   return ss.str();
 }
 struct edge {
   int p, q, w;
 };
 bool cmp(edge &a, edge &b) { return a.w < b.w; }
 template <typename first, typename second>
 ostream &operator<<(ostream &os, const pair<first, second> &p) {
   return os <<  (  << p.first <<  ,   << p.second <<  ) ;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const vector<T> &v) {
   os <<  { ;
   for (auto it = v.begin(); it != v.end(); ++it) {
     if (it != v.begin()) os <<  ,  ;
     os << *it;
   }
   return os <<  } ;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const set<T> &v) {
   os <<  [ ;
   for (auto it = v.begin(); it != v.end(); ++it) {
     if (it != v.begin()) os <<  ,  ;
     os << *it;
   }
   return os <<  ] ;
 }
 template <typename T>
 ostream &operator<<(ostream &os, const multiset<T> &v) {
   os <<  [ ;
   for (auto it = v.begin(); it != v.end(); ++it) {
     if (it != v.begin()) os <<  ,  ;
     os << *it;
   }
   return os <<  ] ;
 }
 template <typename first, typename second>
 ostream &operator<<(ostream &os, const map<first, second> &v) {
   os <<  [ ;
   for (auto it = v.begin(); it != v.end(); ++it) {
     if (it != v.begin()) os <<  ,  ;
     os << it->first <<   =   << it->second;
   }
   return os <<  ] ;
 }
 clock_t tStart = clock();
 void faltu() { cerr << '
'; }
 template <typename T>
 void faltu(T a[], int n) {
   for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
   cerr << '
';
 }
 template <typename T, typename... hello>
 void faltu(T arg, const hello &...rest) {
   cerr << arg << ' ';
   faltu(rest...);
 }
 const int mx = 1e5 + 5;
 bool cnt[mx];
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ;
   int n, k;
   cin >> n >> k;
   long long g;
   cin >> g;
   for (int i = 1; i < n; i++) {
     long long x;
     cin >> x;
     g = gcd(x, g);
   }
   vector<long long> st;
   for (long long i = 0, j = 0; i < k; i++, j += g) {
     int z = j % k;
     if (!cnt[z]) st.emplace_back(z), cnt[z] = 1;
   }
   cout << st.size() << '
';
   for (int i = 0; i < k; i++) {
     if (cnt[i]) cout << i <<    ;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1 << 18;
 const int MOD = 2013265921, MAX = MOD / 2;
 const int gen = 440564289;
 long long power(long long x, int n) {
   if (n <= 1) return n ? x : 1LL;
   long long t = power(x, n / 2);
   if (n & 1)
     return (t * t % MOD) * x % MOD;
   else
     return t * t % MOD;
 }
 long long tmp[maxn];
 void DFT(long long *a, long long x, int n) {
   if (n == 1) {
     a[0] %= MOD;
     if (a[0] < 0) a[0] += MOD;
     return;
   }
   for (int i = 0; i < n; i++) tmp[i] = a[i];
   for (int i = 0; i < n; i++) a[i % 2 ? n / 2 + i / 2 : i / 2] = tmp[i];
   long long *a1 = a, *a2 = a + n / 2, xx = x * x % MOD;
   DFT(a1, xx, n / 2);
   DFT(a2, xx, n / 2);
   long long now = 1;
   for (int i = 0; i < n / 2; i++, now = now * x % MOD) {
     long long val = now * a2[i] % MOD;
     tmp[i] = a1[i] + val - MOD;
     tmp[i + n / 2] = a1[i] - val;
   }
   for (int i = 0; i < n; i++) a[i] = (tmp[i] < 0 ? tmp[i] + MOD : tmp[i]);
 }
 void mul(long long *a, long long *b, long long *c, int n) {
   long long x = power(gen, (1 << 27) / n), xinv = power(x, n - 1);
   long long ninv = power(n, MOD - 2);
   DFT(a, x, n);
   DFT(b, x, n);
   for (int i = 0; i < n; i++) c[i] = a[i] * b[i] % MOD;
   DFT(c, xinv, n);
   for (int i = 0; i < n; i++) {
     c[i] = c[i] * ninv % MOD;
     if (c[i] > MAX) c[i] -= MOD;
   }
 }
 int n, m, Q, L;
 char s[maxn], t[maxn];
 unsigned long long sbit[maxn], tbit[maxn];
 int match(int x, int y, int len) {
   int ret = 0;
   for (; len >= 64; len -= 64, x += 64, y += 64)
     ret += __builtin_popcountll(sbit[x] ^ tbit[y]);
   if (len)
     ret +=
         __builtin_popcountll((sbit[x] << (64 - len)) ^ (tbit[y] << (64 - len)));
   return ret;
 }
 long long *poly[maxn], a[maxn], b[maxn];
 void precal() {
   n = strlen(s), m = strlen(t);
   for (int i = 0; i < n; i++)
     for (int j = 0; j < 64 && i + j < n; j++)
       if (s[i + j] == '1') sbit[i] |= (1ULL << j);
   for (int i = 0; i < m; i++)
     for (int j = 0; j < 64 && i + j < m; j++)
       if (t[i + j] == '1') tbit[i] |= (1ULL << j);
   L = n * sqrt(150 * log2(n) / Q);
   int n2 = 1;
   while (n2 < L + m) n2 *= 2;
   for (int i = 0; i < n; i += L) {
     poly[i / L] = new long long[n2];
     memset(a, 0, sizeof(a));
     memset(b, 0, sizeof(b));
     for (int j = 0; j < L && i + j < n; j++) a[j] = (s[i + j] == '0' ? -1 : 1);
     for (int j = 0; j < m; j++) b[m - 1 - j] = (t[j] == '0' ? -1 : 1);
     mul(a, b, poly[i / L], n2);
   }
 }
 int main() {
   scanf( %s%s%d , s, t, &Q);
   precal();
   while (Q--) {
     int x, y, l;
     scanf( %d%d%d , &x, &y, &l);
     if (l <= L) {
       printf( %d
 , match(x, y, l));
       continue;
     }
     int ans = 0;
     if (x % L) {
       int l2 = L - x % L;
       ans += match(x, y, l2);
       x += l2;
       y += l2;
       l -= l2;
     }
     if ((x + l) % L) {
       int l2 = (x + l - 1) % L + 1;
       ans += match(x + l - l2, y + l - l2, l2);
       l -= l2;
     }
     if (!l) {
       printf( %d
 , ans);
       continue;
     }
     for (int i = x, j = y; l; i += L, j += L, l -= L)
       ans += (L - poly[i / L][m - 1 - j]) / 2;
     printf( %d
 , ans);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int arr[503][503];
 int main() {
   int n, k;
   cin >> n >> k;
   int p = 1;
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j < k; j++) {
       arr[i][j] = p++;
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = k; j <= n; j++) {
       arr[i][j] = p++;
     }
   }
   int sum = 0;
   for (int i = 1; i <= n; i++) {
     sum += arr[i][k];
   }
   cout << sum << endl;
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= n; j++) {
       cout << arr[i][j] << ' ';
     }
     cout << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T, typename U>
 std::istream& operator>>(std::istream& i, pair<T, U>& p) {
   i >> p.first >> p.second;
   return i;
 }
 template <typename T>
 std::istream& operator>>(std::istream& i, vector<T>& t) {
   for (auto& v : t) {
     i >> v;
   }
   return i;
 }
 template <typename T, typename U>
 std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
   o << p.first << ' ' << p.second;
   return o;
 }
 template <typename T>
 std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
   if (t.empty()) o << '
';
   for (size_t i = 0; i < t.size(); ++i) {
     o << t[i] <<
 [i == t.size() - 1];
   }
   return o;
 }
 template <typename T>
 using minheap = priority_queue<T, vector<T>, greater<T>>;
 template <typename T>
 using maxheap = priority_queue<T, vector<T>, less<T>>;
 template <typename T>
 bool in(T a, T b, T c) {
   return a <= b && b < c;
 }
 unsigned int logceil(int first) {
   return 8 * sizeof(int) - __builtin_clz(first);
 }
 namespace std {
 template <typename T, typename U>
 struct hash<pair<T, U>> {
   hash<T> t;
   hash<U> u;
   size_t operator()(const pair<T, U>& p) const {
     return t(p.first) ^ (u(p.second) << 7);
   }
 };
 }  // namespace std
 template <typename T, typename F>
 T bsh(T l, T h, const F& f) {
   T r = -1, m;
   while (l <= h) {
     m = (l + h) / 2;
     if (f(m)) {
       l = m + 1;
       r = m;
     } else {
       h = m - 1;
     }
   }
   return r;
 }
 template <typename F>
 double bshd(double l, double h, const F& f, double p = 1e-9) {
   unsigned int r = 3 + (unsigned int)log2((h - l) / p);
   while (r--) {
     double m = (l + h) / 2;
     if (f(m)) {
       l = m;
     } else {
       h = m;
     }
   }
   return (l + h) / 2;
 }
 template <typename T, typename F>
 T bsl(T l, T h, const F& f) {
   T r = -1, m;
   while (l <= h) {
     m = (l + h) / 2;
     if (f(m)) {
       h = m - 1;
       r = m;
     } else {
       l = m + 1;
     }
   }
   return r;
 }
 template <typename F>
 double bsld(double l, double h, const F& f, double p = 1e-9) {
   unsigned int r = 3 + (unsigned int)log2((h - l) / p);
   while (r--) {
     double m = (l + h) / 2;
     if (f(m)) {
       h = m;
     } else {
       l = m;
     }
   }
   return (l + h) / 2;
 }
 template <typename T>
 T gcd(T a, T b) {
   if (a < b) swap(a, b);
   return b ? gcd(b, a % b) : a;
 }
 template <typename T>
 class vector2 : public vector<vector<T>> {
  public:
   vector2() {}
   vector2(size_t a, size_t b, T t = T())
       : vector<vector<T>>(a, vector<T>(b, t)) {}
 };
 template <typename T>
 class vector3 : public vector<vector2<T>> {
  public:
   vector3() {}
   vector3(size_t a, size_t b, size_t c, T t = T())
       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
 };
 template <typename T>
 class vector4 : public vector<vector3<T>> {
  public:
   vector4() {}
   vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
 };
 template <typename T>
 class vector5 : public vector<vector4<T>> {
  public:
   vector5() {}
   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
 };
 class vowels {
  public:
   void solve(istream& cin, ostream& cout) {
     int N;
     cin >> N;
     vector<int> D(1 << 24, 0);
     for (int i = 0; i < N; ++i) {
       string S;
       cin >> S;
       int u = 0;
       for (char c : S) u |= (1 << (c - 'a'));
       u ^= (1 << 24) - 1;
       D[u]++;
     }
     for (int i = 0; i < 24; ++i) {
       for (int j = 0; j < (1 << 24); ++j) {
         if (j & (1 << i)) D[j ^ (1 << i)] += D[j];
       }
     }
     int ans = 0;
     for (int i = 0; i < (1 << 24); ++i) {
       ans ^= (N - D[i]) * (N - D[i]);
     }
     cout << ans << endl;
   }
 };
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cout.tie(nullptr);
   vowels solver;
   std::istream& in(std::cin);
   std::ostream& out(std::cout);
   solver.solve(in, out);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 310, INF = (1 << 27);
 int d[MAXN][MAXN];
 int all[20][MAXN][MAXN];
 int ans[MAXN][MAXN];
 int main() {
   ios::sync_with_stdio(false);
   int n, m;
   cin >> n >> m;
   for (int i = 0; i < n; i++)
     for (int j = 0; j < n; j++) d[i][j] = INF;
   for (int i = 0; i < n; i++) d[i][i] = 0;
   for (int i = 0; i < m; i++) {
     int x, y, w1, w2;
     cin >> x >> y >> w1 >> w2;
     x--;
     y--;
     w1 = -w1;
     w2 = -w2;
     d[x][y] = w1;
     d[y][x] = w2;
   }
   for (int i = 0; i < n; i++)
     for (int j = 0; j < n; j++) all[0][i][j] = ans[i][j] = d[i][j];
   for (int k = 0; k < n; k++)
     for (int i = 0; i < n; i++)
       for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
   bool hasNeg = false;
   for (int i = 0; i < n; i++)
     if (d[i][i] < 0) hasNeg = true;
   if (!hasNeg) {
     cout << 0 << endl;
     return 0;
   }
   int ind;
   for (ind = 1; (1 << ind) < n; ind++) {
     for (int i = 0; i < n; i++)
       for (int j = 0; j < n; j++) {
         all[ind][i][j] = INF;
         for (int k = 0; k < n; k++)
           all[ind][i][j] =
               min(all[ind][i][j], all[ind - 1][i][k] + all[ind - 1][k][j]);
       }
   }
   ind--;
   int length = 1;
   while (ind >= 0) {
     int tmp[MAXN][MAXN];
     for (int i = 0; i < n; i++)
       for (int j = 0; j < n; j++) {
         tmp[i][j] = INF;
         for (int k = 0; k < n; k++)
           tmp[i][j] = min(tmp[i][j], ans[i][k] + all[ind][k][j]);
       }
     hasNeg = false;
     for (int i = 0; i < n; i++)
       if (tmp[i][i] < 0) hasNeg = true;
     if (!hasNeg) {
       for (int i = 0; i < n; i++)
         for (int j = 0; j < n; j++) ans[i][j] = tmp[i][j];
       length += (1 << ind);
     }
     ind--;
   }
   cout << length + 1 << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 2e6 + 7;
 const int mod = 51123987;
 const int inv2 = mod + 1 >> 1;
 struct Edge {
   int to, last, v;
 } E[maxn];
 int fa[maxn];
 int len[maxn];
 int dep[maxn];
 int head[maxn];
 int tot;
 int last;
 int cnt;
 int n;
 char s[maxn];
 int pre[maxn];
 long long suf[maxn];
 void Init() {
   for (int i = 0; i <= tot; ++i) {
     fa[i] = len[i] = dep[i] = 0;
     head[i] = 0;
   }
   cnt = 0;
   fa[last = 0] = fa[tot = 1] = 1;
   len[1] = -1;
 }
 int Getfail(int x, int ed) {
   while (s[ed] ^ s[ed - len[x] - 1]) x = fa[x];
   return x;
 }
 int Check(int x, int v) {
   for (int i = head[x], y; y = E[i].to, i; i = E[i].last)
     if (E[i].v == v) return E[i].to;
   return 0;
 }
 void Adde(int x, int y, int z) {
   E[++cnt] = (Edge){y, head[x], z};
   head[x] = cnt;
 }
 void Extend(int x, int ed) {
   int p = Getfail(last, ed);
   int np = Check(p, x);
   if (!np) {
     np = ++tot;
     int pp = Getfail(fa[p], ed);
     fa[np] = Check(pp, x);
     Adde(p, np, x);
     len[np] = len[p] + 2;
     dep[np] = dep[fa[np]] + 1;
   }
   last = np;
 }
 int main() {
   Init();
   scanf( %d , &n);
   scanf( %s , s + 1);
   for (int i = 1; i <= n; ++i) Extend(s[i] - 'a', i), pre[i] = dep[last];
   Init();
   reverse(s + 1, s + n + 1);
   for (int i = 1; i <= n; ++i)
     Extend(s[i] - 'a', i), suf[n - i + 1] = dep[last];
   for (int i = n; i; --i) suf[i] += suf[i + 1], suf[i] %= mod;
   int ans = 0;
   for (int i = 1; i <= n; ++i) (ans += 1LL * pre[i] * suf[i + 1] % mod) %= mod;
   int sum = 1LL * suf[1] * (suf[1] - 1) / 2 % mod;
   ans = (sum - ans) % mod;
   ans < 0 ? ans += mod : 0;
   printf( %d
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool vis[200005];
 vector<int> v[200005], ak, d1, dn;
 vector<int> bfs(int n) {
   queue<int> q;
   vector<int> dis(200005);
   q.push(n);
   vis[n] = true;
   dis[n] = 0;
   while (!q.empty()) {
     int x = q.front();
     q.pop();
     for (int i = 0; i < v[x].size(); i++) {
       int u = v[x][i];
       if (vis[u] == false) {
         vis[u] = true;
         dis[u] = dis[x] + 1;
         q.push(u);
       }
     }
   }
   return dis;
 }
 bool cmp(int x, int y) { return d1[x] < d1[y]; }
 int main() {
   int n, m, k, n1, n2, sp, ans = 0;
   scanf( %d %d %d , &n, &m, &k);
   for (int i = 0; i < k; i++) {
     scanf( %d , &sp);
     ak.push_back(sp);
   }
   for (int i = 0; i < m; i++) {
     scanf( %d %d , &n1, &n2);
     v[n1].push_back(n2);
     v[n2].push_back(n1);
   }
   d1 = bfs(1);
   memset(vis, false, sizeof(vis));
   dn = bfs(n);
   sort(ak.begin(), ak.end(), cmp);
   for (int i = 1; i < ak.size(); i++) {
     int x = ak[i - 1];
     int y = ak[i];
     ans = max(ans, d1[x] + 1 + dn[y]);
   }
   ans = min(ans, d1[n]);
   printf( %d
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAX_N = 1050;
 pair<int, int> a[MAX_N];
 int cnt[MAX_N];
 int sub[MAX_N];
 map<int, int> ids;
 int inds[MAX_N];
 int id = 0;
 int countplz(int f0, int f1) {
   memset((sub), 0, sizeof(sub));
   ++sub[a[f0].second];
   ++sub[a[f1].second];
   int ans = 2;
   while (true) {
     int fnval = a[f0].first + a[f1].first;
     auto it = ids.find(fnval);
     if (it == ids.end()) {
       return ans;
     }
     int fnid = it->second;
     int fn = inds[fnid];
     int rest = cnt[fnid] - sub[fnid];
     if (rest <= 0) {
       return ans;
     }
     ++ans;
     sub[fnid]++;
     f0 = f1;
     f1 = fn;
   }
 }
 int main() {
   int n;
   cin >> n;
   for (int i = 0; i < n; ++i) {
     int x;
     cin >> x;
     if (ids.count(x) == 0) {
       ids[x] = id;
       inds[id] = i;
       ++id;
     }
     a[i] = make_pair(x, ids[x]);
     cnt[ids[x]]++;
   }
   int maxs = 2;
   if (ids.count(0) > 0) {
     maxs = max(maxs, cnt[ids[0]]);
   }
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < n; ++j) {
       if (i == j || (a[i].first == a[j].first && a[i].first == 0)) {
         continue;
       }
       maxs = max(maxs, countplz(i, j));
     }
   }
   cout << maxs << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 class TaskB {
  public:
   long long dist(pair<long long, long long> f, pair<long long, long long> s) {
     return (f.first - s.first) * (f.first - s.first) +
            (f.second - s.second) * (f.second - s.second);
   }
   void solve(std::istream& cin, std::ostream& cout) {
     ios_base::sync_with_stdio(0);
     cin.tie(0);
     cout.tie(0);
     vector<pair<long long, long long> > v(3);
     set<pair<long long, long long> > st;
     for (long long i = 0; i < 3; i++) {
       cin >> v[i].first >> v[i].second;
     }
     vector<pair<long long, long long> > ans;
     if (1 || dist(v[1], v[2]) <= dist(v[0], v[1]) + dist(v[0], v[2])) {
       ans.push_back({v[1].first + v[2].first - v[0].first,
                      v[1].second + v[2].second - v[0].second});
     }
     if (1 || dist(v[0], v[2]) <= dist(v[2], v[1]) + dist(v[1], v[0])) {
       ans.push_back({v[0].first + v[2].first - v[1].first,
                      v[0].second + v[2].second - v[1].second});
     }
     if (1 || dist(v[1], v[0]) <= dist(v[2], v[1]) + dist(v[0], v[2])) {
       ans.push_back({v[1].first + v[0].first - v[2].first,
                      v[1].second + v[0].second - v[2].second});
     }
     cout << ans.size() << endl;
     for (long long i = 0; i < ans.size(); i++)
       cout << ans[i].first << ' ' << ans[i].second << endl;
   }
 };
 int main() {
   TaskB solver;
   std::istream& in(std::cin);
   std::ostream& out(std::cout);
   solver.solve(in, out);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n;
 string a, b;
 set<int> A, B;
 queue<int> qA, qB;
 map<char, queue<int> > q;
 vector<pair<int, int> > resp;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cin >> n >> a >> b;
   for (int i = 0; i < n; ++i) {
     if (a[i] == '?')
       qA.push(i);
     else {
       q[a[i]].push(i);
       A.insert(i);
     }
   }
   for (int i = 0; i < n; ++i) {
     if (b[i] == '?')
       qB.push(i);
     else
       B.insert(i);
   }
   for (int i = 0; i < n; ++i) {
     if (!q[b[i]].empty()) {
       resp.push_back({q[b[i]].front(), i});
       B.erase(i);
       A.erase(q[b[i]].front());
       q[b[i]].pop();
     }
   }
   while (!qA.empty() && !B.empty()) {
     auto it = B.begin();
     resp.push_back({qA.front(), *it});
     qA.pop();
     B.erase(it);
   }
   while (!qB.empty() && !A.empty()) {
     auto it = A.begin();
     resp.push_back({*it, qB.front()});
     qB.pop();
     A.erase(it);
   }
   while (!qA.empty() && !qB.empty()) {
     resp.push_back({qA.front(), qB.front()});
     qA.pop();
     qB.pop();
   }
   cout << (int)resp.size() << '
';
   for (auto x : resp) cout << x.first + 1 << ' ' << x.second + 1 << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   int n;
   double p[111];
   cin >> n;
   for (int i = 0; i < n; i++) cin >> p[i];
   sort(p, p + n);
   double ans = p[n - 1], notAns = 1 - p[n - 1];
   for (int i = n - 2; i >= 0 && ans < notAns; i--) {
     ans = ans * (1 - p[i]) + notAns * p[i];
     notAns *= (1 - p[i]);
   }
   printf( %.12lf
 , ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long inf = 1e18;
 long long first, T, mt;
 long long t[2], p[2], a[2];
 long long get(long long ile, long long czas) {
   if (ile == 0) {
     return 0;
   }
   if (min(ile * mt, ile * t[1]) > czas) {
     return inf;
   }
   if (ile * mt <= czas) {
     return 0;
   }
   long long st = 0, en = ile, mid = (st + en) / 2, res = inf;
   while (st <= en) {
     long long il = min(ile, mid * a[1]);
     if (il * t[1] + (ile - il) * mt <= czas) {
       res = min(res, mid);
       en = mid - 1;
     } else {
       st = mid + 1;
     }
     mid = (st + en) / 2;
   }
   return res * p[1];
 }
 int main() {
   scanf( %lld%lld%lld , &first, &T, &mt);
   for (int i = 0; i < 2; i++) {
     scanf( %lld%lld%lld , a + i, t + i, p + i);
   }
   if (t[0] > t[1]) {
     swap(a[0], a[1]);
     swap(t[0], t[1]);
     swap(p[0], p[1]);
   }
   if (min(mt, t[0]) * first > T) {
     printf( -1
 );
     return 0;
   }
   if (mt * first <= T) {
     printf( 0
 );
     return 0;
   }
   long long res = inf;
   for (long long i = 0; (i - 1) * a[0] * t[0] + 1 <= T; i++) {
     long long ile = min(first, a[0] * i);
     long long cena = i * p[0] + get(first - ile, T - ile * t[0]);
     res = min(res, cena);
   }
   printf( %lld
 , res);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t, T, i, n, k, d, ans, val;
   cin >> T;
   vector<int> id(pow(10, 6) + 1, 0);
   for (t = 0; t < T; t++) {
     cin >> n >> k >> d;
     vector<int> v(n);
     for (i = 0; i < n; i++) cin >> v[i];
     ans = n;
     val = 0;
     for (i = 0; i < d; i++) {
       id[v[i]]++;
       if (id[v[i]] == 1) val++;
     }
     ans = val;
     for (i = d; i < n; i++) {
       id[v[i - d]]--;
       if (id[v[i - d]] == 0) val--;
       id[v[i]]++;
       if (id[v[i]] == 1) val++;
       ans = min(val, ans);
     }
     for (i = n - d; i < n; i++) id[v[i]] = 0;
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 const int dx[] = {1, 0, -1, 0};
 const int dy[] = {0, 1, 0, -1};
 using namespace std;
 const int MAXN = 55;
 int n;
 int a[2][MAXN];
 int asum[2][MAXN];
 int b[MAXN];
 int main() {
   cin.tie(0);
   ios::sync_with_stdio(false);
   cin >> n;
   for (int j = 0; j < 2; j++)
     for (int i = 0; i < n - 1; i++) cin >> a[j][i];
   for (int i = 0; i < n; i++) cin >> b[i];
   for (int i = 1; i < n; i++) asum[0][i] = asum[0][i - 1] + a[0][i - 1];
   for (int i = n - 2; i >= 0; i--) asum[1][i] = asum[1][i + 1] + a[1][i];
   vector<int> length;
   for (int i = 0; i < n; i++) length.push_back(asum[0][i] + asum[1][i] + b[i]);
   sort(length.begin(), length.end());
   cout << length[0] + length[1] << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 100010;
 int num[N], tot;
 int main() {
   int n, a1 = 2, a2 = 1, ans = 1;
   cin >> n;
   while (a1 - a2 <= n) {
     if (n % (a1 - a2) == 0) ans = a1 - a2;
     a1 *= 4;
     a2 *= 2;
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int k;
 int values[107];
 int n = 0;
 int ans[107][107];
 int cmb[107];
 int main() {
   int i, j, base;
   cin >> k;
   for (i = 3; i <= 100; i++) values[i] = i * (i - 1) * (i - 2) / 6;
   for (i = 2; i <= 100; i++) cmb[i] = i * (i - 1) / 2;
   i = 3;
   while (values[i] < k) i++;
   if (values[i] > k) i--;
   k -= values[i];
   n = i;
   for (i = 1; i <= n; i++)
     for (j = 1; j <= n; j++)
       if (i != j) ans[i][j] = 1;
   while (k) {
     for (i = 2; cmb[i] < k; i++)
       ;
     if (cmb[i] > k) i--;
     k -= cmb[i];
     n++;
     for (j = 1; j <= i; j++) ans[n][j] = ans[j][n] = 1;
   }
   cout << n << '
';
   for (i = 1; i <= n; i++) {
     for (j = 1; j <= n; j++) cout << ans[i][j];
     cout << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct treenode {
   long long a, b, c;
   treenode(long long a, long long b, long long c) : a(a), b(b), c(c) {}
 };
 int n, mm, s[111111], m[111111], r[111111], zero[111111], *add;
 vector<treenode> tree1[4 * 111111], tree2[4 * 111111], *tree;
 set<pair<int, int> > seg;
 void mergetrees(vector<treenode> &v1, vector<treenode> &v2,
                 vector<treenode> &res) {
   for (int i = 0, j = 0;;) {
     if (v1[i].a > v2[j].a)
       res.push_back(treenode(v1[i].a,
                              v1[i].b + v2[j].b + v2[j].c * (v1[i].a - v2[j].a),
                              v2[j].c + v1[i].c));
     else
       res.push_back(treenode(v2[j].a,
                              v2[j].b + v1[i].b + v1[i].c * (v2[j].a - v1[i].a),
                              v1[i].c + v2[j].c));
     if (v1[i].a == 2000000000) break;
     if (v1[i + 1].a > v2[j + 1].a)
       j++;
     else if (v1[i + 1].a < v2[j + 1].a)
       i++;
     else
       i++, j++;
   }
 }
 void build(int node, int vl, int vr) {
   if (vl == vr) {
     tree[node].push_back(treenode(0, add[vl], r[vl]));
     if (r[vl])
       tree[node].push_back(treenode((m[vl] - add[vl]) / r[vl] + 1, m[vl], 0));
     tree[node].push_back(treenode(2000000000, r[vl] ? m[vl] : add[vl], 0));
   } else {
     build(node * 2, vl, (vl + vr) / 2);
     build(node * 2 + 1, (vl + vr) / 2 + 1, vr);
     mergetrees(tree[node * 2], tree[node * 2 + 1], tree[node]);
   }
 }
 long long getsum(int t, int node, int vl, int vr, int l, int r) {
   if (l > vr || r < vl)
     return 0;
   else if (vl >= l && vr <= r) {
     vector<treenode> &v = tree[node];
     int l = 0, r = (int)v.size() - 1;
     while (l != r) {
       if (v[(l + r) / 2 + 1].a > t)
         r = (l + r) / 2;
       else
         l = (l + r) / 2 + 1;
     }
     return tree[node][l].b + tree[node][l].c * (t - tree[node][l].a);
   } else {
     return getsum(t, node * 2, vl, (vl + vr) / 2, l, r) +
            getsum(t, node * 2 + 1, (vl + vr) / 2 + 1, vr, l, r);
   }
 }
 inline long long getdif(int tnow, int tprev, int l, int r) {
   if (tprev == -1)
     tree = tree2;
   else
     tree = tree1;
   return getsum((tprev == -1) ? tnow : tnow - tprev, 1, 0, n - 1, l - 1, r - 1);
 }
 int main() {
   cin >> n;
   for (int i = 0; i < n; i++) scanf( %d%d%d , &s[i], &m[i], &r[i]);
   cin >> mm;
   tree = tree1;
   add = zero;
   build(1, 0, n - 1);
   tree = tree2;
   add = s;
   build(1, 0, n - 1);
   seg.insert(make_pair(1, -1));
   seg.insert(make_pair(n + 1, -1));
   for (int i = 0; i < mm; i++) {
     int tt, ll, rr;
     scanf( %d%d%d , &tt, &ll, &rr);
     auto j = seg.upper_bound(make_pair(ll, 2000000000));
     j--;
     long long ans = 0;
     bool flag = true;
     for (; j->first <= rr;) {
       auto next = j;
       next++;
       ans += getdif(tt, j->second, max(j->first, ll), min(next->first - 1, rr));
       int l1 = j->first, t1 = j->second;
       seg.erase(j);
       if (l1 < ll) seg.insert(make_pair(l1, t1));
       if (flag) seg.insert(make_pair(ll, tt)), flag = false;
       if (next->first - 1 > rr) seg.insert(make_pair(rr + 1, t1));
       j = next;
     }
     printf( extra
 , ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 using ld = long double;
 using ii = pair<ll, ll>;
 using vi = vector<ll>;
 using vb = vector<bool>;
 using vvi = vector<vi>;
 using vii = vector<ii>;
 using vvii = vector<vii>;
 constexpr int INF = 2000000000;
 constexpr ll LLINF = 9000000000000000000;
 struct ConvexHullSet {
   struct Line {
     ld a, b;
     mutable ld x;
     bool type;
     bool operator<(const Line &rhs) const {
       return type || rhs.type ? x < rhs.x : a < rhs.a;
     }
     ld intersect(const Line &rhs) const {
       return ld(b - rhs.b) / ld(rhs.a - a);
     }
   };
   set<Line> lines;
   static constexpr ld MAX = std::numeric_limits<ld>::max();
   ld query(ld x) {
     auto it = lines.lower_bound(Line{0.0, 0.0, x, true});
     return (it != lines.end() ? it->a * x + it->b : -1e300);
   }
   void adjust(set<Line>::iterator it) {
     if (it != lines.end())
       it->x = next(it) != lines.end() ? it->intersect(*next(it)) : MAX;
     if (it != lines.begin())
       prev(it)->x = it != lines.end() ? it->intersect(*prev(it)) : MAX;
   }
   void insert(ld a, ld b) {
     Line ln = Line{a, b, 0.0, false};
     auto it1 = lines.lower_bound(ln);
     if (it1 != lines.end() && it1->a == a) {
       if (it1->b >= b) return;
       it1 = lines.erase(it1);
       adjust(it1);
     }
     ln.x = it1 != lines.end() ? ln.intersect(*it1) : MAX;
     while (it1 != lines.end() && ln.x >= it1->x) {
       it1 = lines.erase(it1);
       ln.x = it1 != lines.end() ? it1->intersect(ln) : MAX;
       adjust(it1);
     }
     while (it1 != lines.begin()) {
       --it1;
       ld nx = it1->intersect(ln);
       if (nx >= it1->x) return;
       if (it1 != lines.begin() && prev(it1)->x >= nx) {
         it1 = lines.erase(it1);
         adjust(it1);
       } else
         break;
     }
     it1 = lines.insert(ln).first;
     adjust(it1);
   }
 };
 constexpr int L = 0, R = 1000000;
 vector<ld> solve(vii &pairs, int s) {
   vector<ld> d(R - L + 1, 1e300);
   sort(pairs.begin(), pairs.end());
   ConvexHullSet chs;
   for (int i = 0, p = L; p <= R; ++p) {
     while (i < (int)pairs.size() && pairs[i].first == p) {
       int x = pairs[i].first, v = pairs[i].second;
       ld a = 1.0 / ld(s - v) - ld(v) / (ld(s - v) * ld(s + v));
       ld b = -ld(x) / ld(s - v) + ld(x) / ld(s + v) +
              ld(v) * ld(x) / (ld(s - v) * ld(s + v));
       chs.insert(-a, -b);
       ++i;
     }
     d[p] = -chs.query(p);
   }
   return d;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n, s;
   cin >> n >> s;
   vi x(n, 0), v(n, 0), t(n, 0);
   ld lr = 1e300, rr = 1e300;
   int minl = R + 1, maxr = L - 1;
   for (int i = 0; i < n; ++i) {
     cin >> x[i] >> v[i] >> t[i];
     if (t[i] == 1) {
       lr = min(lr, ld(x[i]) / ld(v[i]));
       if (x[i] < minl) minl = x[i];
     } else {
       rr = min(rr, ld(R - x[i]) / ld(v[i]));
       if (x[i] > maxr) maxr = x[i];
     }
   }
   cerr <<  lr =   << lr <<  , rr =   << rr << endl;
   vector<ld> lp, rp;
   {
     vii pairs[2];
     for (int i = 0; i < n; ++i) pairs[t[i] - 1].push_back({x[i], v[i]});
     for (ii &pr : pairs[1]) pr.first = R - pr.first;
     lp = solve(pairs[0], s);
     rp = solve(pairs[1], s);
     reverse(rp.begin(), rp.end());
   }
   ld ans = max(lr, rr);
   for (int p = L; p <= R; ++p)
     ans = min(ans, max(min(lr, lp[p]), min(rr, rp[p])));
   printf( %.10lf
 , double(ans));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(0), cin.tie(0);
   int n, m;
   cin >> n >> m;
   vector<vector<int>> adj(n);
   for (int i = 0, u, v; i < m; ++i) {
     cin >> u >> v;
     --u, --v;
     adj[u].push_back(v);
     adj[v].push_back(u);
   }
   set<int> go;
   queue<int> q;
   for (int i = 0; i < n; ++i) go.insert(i);
   int ans = 0;
   for (int x = 0; x < n; ++x)
     if (go.find(x) != go.end()) {
       ++ans;
       go.erase(x);
       q.push(x);
       while (!q.empty()) {
         int u = q.front();
         q.pop();
         vector<int> add;
         for (auto v : adj[u])
           if (go.find(v) != go.end()) {
             go.erase(v);
             add.push_back(v);
           }
         for (auto v : go) q.push(v);
         go.clear();
         for (auto v : add) go.insert(v);
       }
     }
   cout << ans - 1 <<
 ;
   return 0;
 }