#include <bits/stdc++.h>
 using namespace std;
 vector<pair<int, int> > g[100010];
 int N, K, in[100010], dp[100010][2];
 vector<pair<int, int> > num[100010];
 void dfs(int u, int fa) {
   for (pair<int, int>& e : g[u]) {
     int v = e.first, cost = e.second;
     if (v == fa) continue;
     dfs(v, u);
     num[u].push_back(pair<int, int>(dp[v][1] + cost, v));
   }
   sort(num[u].begin(), num[u].end(), greater<pair<int, int> >());
   dp[u][1] = 0;
   int sum = 0;
   for (size_t i = 0; (int)i < K - 1 && i < num[u].size(); i++) {
     dp[u][1] += num[u][i].first;
     sum += num[u][i].first;
   }
   if ((int)num[u].size() <= K - 1) {
     for (size_t i = 0; i < num[u].size(); i++) {
       int v = num[u][i].second;
       dp[u][0] = max(dp[u][0], sum - dp[v][1] + dp[v][0]);
     }
   } else {
     for (size_t i = 0; i < (int)K - 1; i++) {
       int v = num[u][i].second;
       dp[u][0] = max(dp[u][0], sum + num[u][K - 1].first - dp[v][1] + dp[v][0]);
     }
     for (size_t i = K - 1; i < num[u].size(); i++) {
       int v = num[u][i].second;
       dp[u][0] = max(dp[u][0], sum + num[u][i].first - dp[v][1] + dp[v][0]);
     }
   }
 }
 int main() {
   int u, v, c;
   scanf('%d%d', &N, &K);
   for (int i = 1; i < N; i++) {
     scanf('%d%d%d', &u, &v, &c);
     g[u].push_back(pair<int, int>(v, c));
     g[v].push_back(pair<int, int>(u, c));
   }
   dfs(0, -1);
   printf('%d
', max(dp[0][0], dp[0][1]));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long GCD(long long a, long long b) {
   while (b) b ^= a ^= b ^= a %= b;
   return a;
 }
 long long LCM(long long a, long long b) { return a * (b / GCD(a, b)); }
 const double EPS = 10e-9;
 const long long INF = (1LL << 30);
 using namespace std;
 template <typename T>
 string toString(T t) {
   stringstream ss;
   ss << t;
   return ss.str();
 }
 long long isSquare(long long N) {
   long long lo = 1, hi = 2 * sqrt(N);
   while (hi - lo > 1) {
     long long mid = (hi + lo) >> 1;
     long long res = mid * mid;
     if (res < N) {
       lo = mid;
     } else if (res > N) {
       hi = mid;
     } else {
       return mid;
     }
   }
   return lo;
 }
 int main() {
   long long N;
   cin >> N;
   if (N % 3 != 0) {
     cout << 0 << endl;
   } else {
     N /= 3;
     int options = 0;
     for (long long i = 1; i * i * i <= N; i++) {
       if (N % i == 0) {
         long long left = N / i, to = i / 2;
         for (long long a = 1; a <= to; a++) {
           long long c = i - a;
           long long d = (a - c) * (a - c);
           long long sqr = isSquare(d + 4 * left);
           if (sqr * sqr == d + 4 * left) {
             long long b = (-(a + c) + sqr) >> 1;
             if (c <= b) {
               if (a == c) {
                 if (b == c) {
                   options += 1;
                 } else {
                   options += 3;
                 }
               } else {
                 if (b == c) {
                   options += 3;
                 } else {
                   options += 6;
                 }
               }
             }
           }
         }
       }
     }
     cout << options << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int mod = 1000000007;
 const long long int inf = 1e17;
 const long long int N = 5005;
 long long int n;
 long long int a[N];
 long long int dp[N][N];
 long long int go(long long int idx, long long int flag) {
   long long int &ans = dp[idx][flag];
   if (ans != -1) return ans;
   if (idx > n) {
     return 0;
   }
   if (flag == 0 or flag == 2) {
     ans = max(go(idx + 1, flag), go(idx + 1, flag + 1)) + a[idx];
   } else {
     ans = max(go(idx + 1, flag), go(idx + 1, flag + 1)) - a[idx];
   }
   return ans;
 }
 vector<long long int> res;
 void backtrack(long long int idx, long long int flag) {
   if (idx > n) {
     return;
   }
   if (flag == 0 or flag == 2) {
     long long int tmp1 = go(idx + 1, flag) + a[idx];
     long long int tmp2 = go(idx + 1, flag + 1) + a[idx];
     res.push_back(1);
     if (tmp1 >= tmp2) {
       backtrack(idx + 1, flag);
     } else {
       backtrack(idx + 1, flag + 1);
     }
   } else {
     long long int tmp1 = go(idx + 1, flag) - a[idx];
     long long int tmp2 = go(idx + 1, flag + 1) - a[idx];
     res.push_back(0);
     if (tmp1 >= tmp2) {
       backtrack(idx + 1, flag);
     } else {
       backtrack(idx + 1, flag + 1);
     }
   }
 }
 int32_t main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   memset(dp, -1, sizeof(dp));
   cin >> n;
   for (long long int i = 1; i <= n; i++) {
     cin >> a[i];
   }
   long long int ans = go(0, 0);
   backtrack(0, 0);
   res.erase(res.begin());
   long long int cnt = 0, idx = 0;
   while (res[idx] and idx < n) {
     cnt++;
     idx++;
   }
   vector<long long int> vec;
   vec.push_back(cnt);
   cnt = 0;
   idx = vec.back();
   while (idx < n and !res[idx]) {
     cnt++;
     idx++;
   }
   vec.push_back(cnt);
   idx = vec[0] + vec[1];
   cnt = 0;
   while (idx < n and res[idx]) {
     cnt++;
     idx++;
   }
   vec.push_back(cnt);
   idx = 0, cnt = 0;
   for (auto to : vec) {
     cnt = to;
     if (cnt == 0LL) {
       cout << idx << ' ';
     } else {
       cout << idx + cnt << ' ';
       idx += cnt;
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e5 + 10;
 bool chk(string s) {
   int l = s.size();
   for (int i = 0, j = l - 1; i <= j; i++, j--) {
     if (s[i] != s[j]) return false;
   }
   return true;
 }
 int main() {
   int t;
   cin >> t;
   while (t--) {
     string s;
     cin >> s;
     string ans;
     int mx = 0;
     string res;
     for (int i = 0; i < s.size(); i++) {
       ans += s[i];
       if (chk(ans)) {
         int l = ans.size();
         if (l > mx) {
           mx = ans.size();
           res = ans;
         }
       }
     }
     ans = '';
     for (int i = s.size() - 1; i >= 0; i--) {
       ans += s[i];
       if (chk(ans)) {
         int l = ans.size();
         if (l > mx) {
           mx = ans.size();
           res = ans;
         }
       }
     }
     int p1 = 0;
     int p2 = 0;
     for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
       if (s[i] != s[j]) {
         p1 = i - 1;
         p2 = j + 1;
         break;
       }
     }
     ans = '';
     string rev = '';
     for (int i = 0; i <= p1; i++) ans += s[i];
     for (int i = p2; i < s.size(); i++) rev += s[i];
     string ss = '';
     for (int i = p1 + 1; i < p2; i++) {
       ss += s[i];
       string tmp = ans + ss + rev;
       int l = tmp.size();
       if (l <= mx) continue;
       if (chk(tmp)) {
         if (l > mx) {
           mx = l;
           res = tmp;
         }
       }
     }
     ss = '';
     for (int j = p2 - 1; j > p1; j--) {
       ss = s[j] + ss;
       string tmp = ans + ss + rev;
       int l = tmp.size();
       if (chk(tmp)) {
         if (l > mx) {
           mx = l;
           res = tmp;
         }
       }
     }
     cout << res << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int n, m;
   scanf('%d%d', &n, &m);
   int num = n + m;
   int a = m / 2, aa = m % 2, aaa = a + aa;
   if (n == m) {
     for (int i = 0; i < n; i++) {
       printf('01');
     }
   } else if (n < m) {
     if (aaa <= n + 1) {
       a = m - (n + 1);
       m -= a;
       for (int i = 0; n; i++) {
         if (a) {
           printf('11');
           a--;
         } else if (m) {
           printf('1');
           m--;
         }
         if (n) {
           printf('0');
           n--;
         }
       }
       if (a)
         printf('11');
       else if (m)
         printf('1');
     } else
       printf('-1');
   } else if (n > m) {
     if (n <= m + 1) {
       for (int i = 0; i < m; i++) {
         printf('01');
       }
       printf('0');
     } else
       printf('-1');
   }
   printf('
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long k, d, t;
 int main() {
   scanf('%lld%lld%lld', &k, &d, &t);
   t *= 2;
   long long round = ceil((double)k / d) * d;
   long long num = k * 2 + (round - k);
   long long cnt = t / num;
   long long left = t - cnt * num;
   double ans = 0.0;
   if (left <= k * 2) {
     ans = cnt * round + left / 2;
     if (left % 2) ans += 0.5;
   } else
     ans = cnt * round + k + (left - k * 2);
   printf('%.1lf
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   int ans = 0, last = 0, current = 0;
   while (n > 0) {
     ans++;
     current = last + ans;
     last = current;
     n -= current;
     if (n < 0) {
       ans--;
     }
   }
   cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 int head, tail, n, q[100100];
 long long f[100100], a[100100], b[100010];
 int main() {
   scanf('%d', &n);
   for (int i = 1; i <= n; i++) scanf('%I64d', &a[i]);
   for (int i = 1; i <= n; i++) scanf('%I64d', &b[i]);
   f[1] = 0;
   head = tail = 1;
   q[head] = 1;
   for (int i = 2; i <= n; i++) {
     while (head < tail) {
       if ((1.0 * f[q[head]] + 1.0 * a[i] * b[q[head]]) >
           (1.0 * f[q[head + 1]] + 1.0 * a[i] * b[q[head + 1]]))
         head++;
       else
         break;
     }
     f[i] = f[q[head]] + b[q[head]] * a[i];
     while (head < tail) {
       if (1.0 * (f[q[tail]] - f[q[tail - 1]]) * (b[q[tail]] - b[i]) >
           1.0 * (f[i] - f[q[tail]]) * (b[q[tail - 1]] - b[q[tail]]))
         tail--;
       else
         break;
     }
     q[++tail] = i;
   }
   printf('%I64d', f[n]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MOD = 998244353;
 const int N = 5005;
 void update(int &a, int b) {
   a += b;
   a %= MOD;
 }
 int add(int a, int b) { return (a + b) % MOD; }
 int multiply(int a, int b) { return (a * 1LL * b) % MOD; }
 int quick_power(int a, int n) {
   int res = 1;
   for (; n > 0; n >>= 1) {
     if (n & 1) res = multiply(res, a);
     a = multiply(a, a);
   }
   return res;
 }
 int get_inv(int a) { return quick_power(a, MOD - 2); }
 int n, a;
 int cnt[N], sum[N][N], dp[N][N];
 int main() {
   cin >> n;
   for (int i = 1; i <= n; i++) {
     cin >> a;
     cnt[a]++;
   }
   for (int i = 0; i <= n; i++) {
     sum[0][i] = 1;
   }
   int answer = 0;
   for (int i = 1; i <= n; i++) {
     int inv = get_inv(n - i + 1);
     for (int j = 1; j <= n; j++) {
       if (cnt[j]) {
         dp[i][j] = multiply(multiply(sum[i - 1][j - 1], cnt[j]), inv);
         update(answer, multiply(multiply(dp[i - 1][j], cnt[j] - 1), inv));
       }
       sum[i][j] = add(sum[i][j - 1], dp[i][j]);
     }
   }
   cout << answer << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   long double n, h;
   cin >> n >> h;
   cout << setprecision(20);
   for (long double i = 0; i < n - 1; i++) {
     cout << h * sqrt((i + 1) / n) << ' ';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const double pi = acos(-1);
 const long long N = 1e5 + 5, M = 1e9 + 7;
 char ulta[500];
 set<char> ache;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   ulta['U'] = 'D';
   ulta['D'] = 'U';
   ulta['R'] = 'L';
   ulta['L'] = 'R';
   string s;
   long long n, i, j, k, ans = 2;
   cin >> n >> s;
   for (auto ch : s) {
     auto x = ache.find(ulta[ch]);
     if (x != ache.end()) {
       ans++;
       ache.clear();
     }
     ache.insert(ch);
   }
   cout << ans - 1 << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct State {
   int len, link;
   map<char, int> nxtc;
 } SA[2 * 2006];
 bool vis[2 * 2006];
 char str[2006], pref[2 * 2006], suf[2006];
 class SuffixAutomata {
   int sz, last;
   void init(int idx) {
     SA[idx].len = 0, SA[idx].link = -1;
     SA[idx].nxtc.clear();
   }
   bool traverse(int cur, char *ch) {
     if (*ch == 0) return true;
     if (SA[cur].nxtc.count(*ch) == 0) return false;
     int p = SA[cur].nxtc[*ch];
     return traverse(p, ++ch);
   }
   int findState(int cur, char *ch) {
     if (*ch == 0) return cur;
     int p = SA[cur].nxtc[*ch];
     return findState(p, ++ch);
   }

  public:
   SuffixAutomata() {
     sz = last = 0;
     init(0);
     ++sz;
   }
   int size() { return sz; }
   void extend(char ch) {
     int p, q, clone, cur = sz++;
     init(cur);
     SA[cur].len = SA[last].len + 1;
     for (p = last; p != -1 && SA[p].nxtc.count(ch) == 0; p = SA[p].link)
       SA[p].nxtc[ch] = cur;
     if (p == -1)
       SA[cur].link = 0;
     else {
       q = SA[p].nxtc[ch];
       if (SA[p].len + 1 == SA[q].len)
         SA[cur].link = q;
       else {
         clone = sz++;
         init(clone);
         SA[clone] = SA[q];
         SA[clone].len = SA[p].len + 1;
         for (; p != -1 && SA[p].nxtc[ch] == q; p = SA[p].link)
           SA[p].nxtc[ch] = clone;
         SA[cur].link = SA[q].link = clone;
       }
     }
     last = cur;
   }
   bool traverse(char *str) { return traverse(0, str); }
   int findState(char *str) { return findState(0, str); }
 };
 int lenp, lens, dp[2 * 2006][2006];
 int rec(int cur, int idx) {
   if (suf[idx] == 0) return 1;
   if (dp[cur][idx] != -1) return dp[cur][idx];
   int &ret = dp[cur][idx];
   ret = 0;
   char ch;
   int p;
   if (idx) {
     ch = suf[idx];
     if (SA[cur].nxtc.count(ch) == 0) return ret = 0;
     p = SA[cur].nxtc[ch];
     return ret = rec(p, idx + 1);
   }
   map<char, int> &M = SA[cur].nxtc;
   map<char, int>::iterator im;
   for (im = M.begin(); im != M.end(); ++im) {
     ch = im->first;
     p = im->second;
     ret += rec(p, idx);
     if (ch == suf[idx]) ret += rec(p, idx + 1);
   }
   return ret;
 }
 int main() {
   int i, j, ans, n;
   while (scanf('%s', str) != EOF) {
     SuffixAutomata sa;
     for (i = 0; str[i]; ++i) sa.extend(str[i]);
     ans = 0;
     scanf('%s %s', pref, suf);
     lenp = strlen(pref), lens = strlen(suf);
     if (!sa.traverse(pref)) {
       puts('0');
       continue;
     }
     for (i = 0; i < lenp; ++i) {
       if (i + lens < lenp) continue;
       for (j = 0; j < lens; ++j) {
         if (pref[i + j] != suf[j]) break;
       }
       if (i + j == lenp) {
         strcat(pref, &suf[lenp - i]);
         ans += sa.traverse(pref);
         pref[lenp] = 0;
       }
     }
     n = sa.findState(pref);
     memset(dp, -1, sizeof(dp));
     ans += rec(n, 0);
     printf('%d
', ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 class base {
  public:
   long double a, b;
   base(void) {
     a = 0;
     b = 0;
   }
   base(long double _a, long double _b) {
     a = _a;
     b = _b;
   }
   base(long double _a) {
     a = _a;
     b = 0;
   }
   base operator*(const base& b2) {
     base ret(a * b2.a - b * b2.b, b * b2.a + a * b2.b);
     return ret;
   }
   base operator+(const base& b2) {
     base ret(a + b2.a, b + b2.b);
     return ret;
   }
   base operator-(const base& b2) {
     base ret(a - b2.a, b - b2.b);
     return ret;
   }
   base operator/(const base& b2) {
     long double c = b2.a, d = b2.b;
     base ret((a * c + b * d) / (c * c + d * d),
              (b * c - a * d) / (c * c + d * d));
     return ret;
   }
   void operator*=(const base& b2) {
     long double tmp = a * b2.a - b * b2.b;
     b = b * b2.a + a * b2.b;
     a = tmp;
   }
   void operator/=(const base& b2) {
     long double c = b2.a, d = b2.b;
     long double tmp = (a * c + b * d) / (c * c + d * d);
     b = (b * c - a * d) / (c * c + d * d);
     a = tmp;
   }
   inline long double real(void) { return a; }
 };
 void fft(vector<base>& a, bool invert) {
   int n = (int)a.size();
   for (int i = 1, j = 0; i < n; ++i) {
     int bit = n >> 1;
     for (; j >= bit; bit >>= 1) j -= bit;
     j += bit;
     if (i < j) swap(a[i], a[j]);
   }
   base ww[n];
   long double ang = 2 * 3.14159265358979323846l / n * (invert ? -1 : 1);
   base wi(cos(ang), sin(ang));
   ww[0] = 1;
   for (int i = 1; i < n; i++) {
     ww[i] = ww[i / 2] * ww[i / 2];
     if (i % 2 == 1) ww[i] *= wi;
   }
   for (int len = 2; len <= n; len <<= 1) {
     int k = n / len;
     for (int i = 0; i < n; i += len) {
       int wn = 0;
       for (int j = 0; j < len / 2; ++j) {
         base u = a[i + j], v = a[i + j + len / 2] * ww[wn];
         a[i + j] = u + v;
         a[i + j + len / 2] = u - v;
         wn += k;
       }
     }
   }
   if (invert)
     for (int i = 0; i < n; ++i) a[i] /= n;
 }
 vector<int> multiply(const vector<int>& a, const vector<int>& b) {
   vector<base> fa(a.begin(), a.end());
   size_t n = 1;
   while (n < (a.size() + b.size())) n <<= 1;
   fa.resize(n);
   vector<int> res;
   fft(fa, false);
   for (size_t i = 0; i < n; ++i) fa[i] *= fa[i];
   fft(fa, true);
   res.resize(n);
   long long int carry = 0;
   for (size_t i = 0; i < n; ++i) {
     carry += (long long int)(fa[i].real() + 0.5);
     res[i] = carry % 1000;
     carry /= 1000;
   }
   int i;
   for (i = n - 1; i > 0; i--) {
     if (res[i] != 0) break;
   }
   res.resize(i + 1);
   return res;
 }
 void small_mult(vector<int>& a, int b) {
   int carry = 0;
   for (int i = 0; i < (int)a.size() || carry; i++) {
     if (i == (int)a.size()) a.push_back(0);
     int cur = carry + a[i] * b;
     a[i] = cur % 1000;
     carry = cur / 1000;
   }
   while (a.size() > 1 && !a.back()) a.pop_back();
 }
 void fast_3_pow(long long int k, vector<int>& ans) {
   ans = {1};
   if (k <= 0) return;
   long long int r = 0, kk = 0;
   for (long long int t = k; t > 0; t /= 2) {
     r = (2 * r + t % 2);
     kk++;
   }
   for (long long int i = 0; i < kk; i++) {
     ans = multiply(ans, ans);
     if (r % 2) small_mult(ans, 3);
     r /= 2;
   }
 }
 bool first_is_BE(vector<int>& v1, vector<int>& v2) {
   if (v1.size() > v2.size()) return 1;
   if (v1.size() < v2.size()) return 0;
   for (long long int i = ((long long int)v1.size()) - 1; i >= 0; i--) {
     if (v1[i] > v2[i]) return 1;
     if (v1[i] < v2[i]) return 0;
   }
   return 1;
 }
 int main() {
   ios::sync_with_stdio(0);
   string s;
   cin >> s;
   if (s == '1') {
     cout << '1';
     return 0;
   }
   if (s == '2') {
     cout << '2';
     return 0;
   }
   if (s == '3') {
     cout << '3';
     return 0;
   }
   if (s == '4') {
     cout << '4';
     return 0;
   }
   if (s == '5') {
     cout << '5';
     return 0;
   }
   if (s == '6') {
     cout << '5';
     return 0;
   }
   if (s == '7') {
     cout << '6';
     return 0;
   }
   if (s == '8') {
     cout << '6';
     return 0;
   }
   if (s == '9') {
     cout << '6';
     return 0;
   }
   vector<int> n;
   int tmp = 0;
   while (s.size() % 3 != 0) s = '0' + s;
   for (int i = 0; i < (int)s.size(); i++) {
     tmp *= 10;
     tmp += s[i] - '0';
     if ((i + 1) % 3 == 0) {
       n.push_back(tmp);
       tmp = 0;
     }
   }
   reverse(n.begin(), n.end());
   vector<int> v1, vt;
   int k;
   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0);
   fast_3_pow(k, v1);
   vt = v1;
   small_mult(v1, 4);
   while (1) {
     if (first_is_BE(v1, n)) {
       break;
     }
     k++;
     small_mult(v1, 3);
   }
   int ans3 = 3 * k + 4;
   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0);
   v1 = vt;
   while (1) {
     if (first_is_BE(v1, n)) {
       break;
     }
     k++;
     small_mult(v1, 3);
   }
   int ans1 = 3 * k;
   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0);
   v1 = vt;
   small_mult(v1, 2);
   while (1) {
     if (first_is_BE(v1, n)) {
       break;
     }
     k++;
     small_mult(v1, 3);
   }
   int ans2 = 3 * k + 2;
   cout << min(min(ans1, ans2), ans3);
   return 0;
 }


#include <bits/stdc++.h>
 #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 #define ll long long int
 #define pb push_back
 #define pob pop_back
 #define ub upper_bound
 #define lb lower_bound
 #define mp make_pair
 #define f0(i,n) for(i=0;i<n;i++)
 #define rf0(i,n) for(i=n-1;i>=0;i--)
 #define f2(i,n) for(i=1;i<n;i++)
 #define f1(i,n) for(i=1;i<=n;i++)
 #define fab(i,a,b) for(i=a;i<=b;i++)
 #define shr  ll t;cin>>t; while(t--)
 #define fi first
 #define sc second
 #define pll pair<ll,ll>
 #define vll vector<ll>
 #define vpll vector<pll>
 #define all(v) v.begin(),v.end()
 #define mod 1000000007
 #define dev(a) for(auto it:a)cout<<it<<' '
 #define prec(nm,prc) cout<<fixed << setprecision(nm)<<prc
 #define print(a) cout<<a<<endl;
 #define nl '
'
 using namespace std;
 bool comp(pll &a,pll &b)
 {
     return (a.sc<b.sc);
 }
 ll power(ll n,ll p)
 {
 	if(p==0)
 	return 1;
 	else
 	{
 		ll ans=power(n,p/2)%mod;
 		ans=(ans*ans)%mod;
 		if(p%2==0)
 		return ans;
 		else
 		return ans=(ans*n)%mod;
 	}
 }
 bool isPrime(ll n)
 {
     if (n <= 1)
         return false;
     if (n <= 3)
         return true;

     if (n % 2 == 0 || n % 3 == 0)
         return false;

     for (ll i = 5; i * i <= n; i = i + 6)
         if (n % i == 0 || n % (i + 2) == 0)
             return false;

     return true;
 }

     /********************HAR HAR MAHADEV***********JAI BAJRANG BALI************************/
   int main()
  {
   	FAST;
   	/* ifstream cin;
     cin.open('input.txt');
     ofstream cout;
     cout.open('output.txt');
                       */
   	shr
   	  {
   	  	ll i,j,n,a,b,c,c1,d,m,sum=0,temp,k,k2,k1,ans=0,cnt=0,r=0,e=0,f;
   	  	string str,stri,om,s;
   	     map<ll,ll>mp,mp1;
   	     set<ll>st; vector<vll> v;
   	     cin>>n>>k; ll x[n][2];
   	     f0(i,n)
   	     {
   	         cin>>x[i][0]>>x[i][1];
   	     }
   	    f0(i,n)
   	    {
           vll vk;
            f0(j,n)
           {

               if(i!=j)
               {
                   vk.pb(abs(x[i][0]-x[j][0])+abs(x[i][1]-x[j][1]));
               }

           }
            v.pb(vk);
           r++;
       }
   	   f0(i,v.size())
   	   {
       ll p=*max_element(all(v[i]));
       if(p<=k){e=1;break;}
       }
       if(e==1)cout<<1;
       else cout<<-1;
       cout<<endl;
   	  }

    	return 0;
   }


#include <bits/stdc++.h>
 #pragma GCC optimize('O3')
 const double pi = acos(-1);
 const int MOD = 1e9 + 7;
 const int INF = 1e9 + 7;
 const int MAXN = 10000;
 const double eps = 1e-9;
 using namespace std;
 int n, m, cur;
 set<int> s;
 void dfs(int x, int y) {
   if (y == 0) {
     int tmp = abs(abs(x) - n);
     if (tmp < MAXN) {
       if (s.size() < m) s.insert(cur * MAXN + tmp);
       if (s.size() < m) s.insert(cur + MAXN * tmp);
     }
     return;
   }
   dfs(x + (y % 10), y / 10);
   dfs(x - (y % 10), y / 10);
   dfs(x * (y % 10), y / 10);
 }
 int main() {
   scanf('%d', &(n)), scanf('%d', &(m));
   for (int i = 0; i < MAXN; i++) {
     cur = i;
     dfs(i % 10, i / 10);
   }
   auto it = s.begin();
   for (; it != s.end(); it++) printf('%08d
', *it);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int num;
 int likes[7][7];
 map<string, int> heroes;
 int getNum(string s) {
   if (!heroes.count(s)) heroes[s] = num++;
   return heroes[s];
 }
 int getMaxSymp(int n1, int n2) {
   vector<int> p, groups(7, 0);
   for (int i = 0; i < 7; i++) {
     p.push_back(i);
     if (i >= n1) groups[i] = i < n1 + n2 ? 1 : 2;
   }
   int res = 0;
   do {
     int r = 0;
     for (int i = 0; i < 7; i++)
       for (int j = 0; j < 7; j++)
         if (groups[i] == groups[j]) r += likes[p[i]][p[j]];
     res = max(r, res);
   } while (next_permutation(p.begin(), p.end()));
   return res;
 }
 int main() {
   int i, j, k, n, a, b, c, res = 2000000000, symp = 0;
   cin >> n;
   for (i = 0; i < n; i++) {
     string s1, s2, s3;
     cin >> s1 >> s3 >> s2;
     likes[getNum(s1)][getNum(s2)] = 1;
   }
   cin >> a >> b >> c;
   for (i = 1; i < 7; i++)
     for (j = 1; j + i < 7; j++) {
       k = 7 - i - j;
       int d = max(a / i, max(b / j, c / k)) - min(a / i, min(b / j, c / k));
       if (d < res)
         res = d, symp = getMaxSymp(i, j);
       else if (res == d)
         symp = max(symp, getMaxSymp(i, j));
     }
   cout << res << ' ' << symp << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long MOD = 998244353;
 long long inv[5050 + 10], fac[5050 + 10], ifac[5050 + 10];
 void setComb() {
   inv[0] = 1;
   inv[1] = 1;
   fac[1] = 1;
   ifac[1] = 1;
   fac[0] = 1;
   ifac[0] = 1;
   for (int i = 2; i < 5050; i++) {
     inv[i] = (-MOD / i) * inv[MOD % i] % MOD;
     fac[i] = fac[i - 1] * i % MOD;
     ifac[i] = ifac[i - 1] * inv[i] % MOD;
     inv[i] = (inv[i] + MOD) % MOD;
     fac[i] = (fac[i] + MOD) % MOD;
     ifac[i] = (ifac[i] + MOD) % MOD;
   }
   return;
 }
 long long comb(long long n, long long k) {
   if (n < k || n < 0 || k < 0)
     return 0;
   else
     return ((fac[n] * ifac[k] % MOD * ifac[n - k] % MOD + MOD) % MOD);
 }
 long long hcomb(long long n, long long r) {
   if (n == 0 && r == 0)
     return 1;
   else if (n < 0 || r < 0)
     return 0;
   else
     return comb(n + r - 1, r - 1);
 }
 int N;
 vector<int> A;
 long long dp[5050][5050];
 long long cnt[5050];
 void add(long long &a, long long b) { a = (a + b) % MOD; }
 void mul(long long &a, long long b) { a = a * b % MOD; }
 int main() {
   cin.tie(0);
   ios::sync_with_stdio(false);
   cin >> N;
   A.resize(N);
   for (int i = 0; i < (N); ++i) cin >> A[i];
   sort(A.begin(), A.end());
   for (int i = 0; i < (N); ++i) cnt[A[i]]++;
   setComb();
   dp[0][0] = 1;
   for (int i = 0; i < N; i++) {
     long long sum = 0;
     for (int j = 0; j <= 5000; j++) {
       long long res = sum;
       mul(res, cnt[j]);
       mul(res, inv[N - i]);
       add(dp[i + 1][j], res);
       sum += dp[i][j];
     }
   }
   long long ans = 0;
   for (int i = 1; i < N; i++) {
     for (int j = 0; j <= 5000; j++) {
       long long res = dp[i][j];
       mul(res, cnt[j] - 1);
       mul(res, inv[N - i]);
       add(ans, res);
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   vector<int> v;
   vector<int>::iterator it;
   int n, q, x, count1 = 0, x1;
   cin >> q;
   while (q--) {
     cin >> n;
     while (n--) {
       cin >> x;
       v.push_back(x);
     }
     it = v.end() - 1;
     x1 = *it;
     for (it = v.end() - 2; it >= v.begin(); it--) {
       if (*it > x1)
         count1++;
       else
         x1 = *it;
     }
     cout << count1 << endl;
     count1 = 0;
     v.clear();
   }
 }


#include <bits/stdc++.h>
 long long ans;
 int F[50000001], n, m, N;
 char str[10001];
 void dfs(int x, int bit, int key) {
   if (x > 3) {
     if (bit) F[bit] += key;
     return;
   }
   dfs(x + 1, bit, key);
   int v = str[x] - 'a';
   dfs(x + 1, bit | (1 << v), -key);
 }
 int main() {
   scanf('%d', &m);
   for (int i = 1; i <= m; i++) {
     scanf('%s', str + 1);
     dfs(1, 0, -1);
   }
   n = 24;
   N = 1 << n;
   int res = 0;
   for (int i = 0; i < N; i++) res += F[i];
   for (int i = 1; i < N; i <<= 1)
     for (int j = 0; j < N; j++)
       if (i & j) {
         F[j] += F[j - i];
       }
   for (int i = 0; i < N; i++) ans ^= (long long)F[i] * F[i];
   printf('%I64d
', ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 10, MAXM = 100;
 int A[MAXN][MAXM], B[MAXN][MAXM], C[MAXN][MAXM];
 int order[MAXM];
 int ib, is;
 bool cmp(int a, int b) { return B[is][a] - A[ib][a] > B[is][b] - A[ib][b]; }
 int main() {
   int n, m, k, ans = 0, tmp;
   int cur_hold, nx, p;
   int i, j, id;
   scanf('%d%d%d', &n, &m, &k);
   for (i = 0; i < n; ++i) {
     scanf('%*s');
     for (j = 0; j < m; ++j) scanf('%d%d%d', A[i] + j, B[i] + j, C[i] + j);
   }
   for (ib = 0; ib < n; ++ib)
     for (is = 0; is < n; ++is)
       if (ib != is) {
         for (j = 0; j < m; ++j) order[j] = j;
         sort(order, order + m, cmp);
         for (tmp = cur_hold = j = 0; j < m; ++j) {
           id = order[j];
           if ((p = B[is][id] - A[ib][id]) > 0) {
             nx = min(C[ib][id], k - cur_hold);
             tmp += p * nx;
             cur_hold += nx;
           } else
             break;
         }
         if (ans < tmp) ans = tmp;
       }
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void init() {}
 void _print(long long int t) { cerr << t; }
 void _print(int t) { cerr << t; }
 void _print(string t) { cerr << t; }
 void _print(char t) { cerr << t; }
 void _print(long double t) { cerr << t; }
 void _print(double t) { cerr << t; }
 void _print(unsigned long long int t) { cerr << t; }
 template <class T, class V>
 void _print(pair<T, V> p);
 template <class T>
 void _print(vector<T> v);
 template <class T>
 void _print(set<T> v);
 template <class T, class V>
 void _print(map<T, V> v);
 template <class T>
 void _print(multiset<T> v);
 template <class T, class V>
 void _print(pair<T, V> p) {
   cerr << '{';
   _print(p.first);
   cerr << ',';
   _print(p.second);
   cerr << '}';
 }
 template <class T>
 void _print(vector<T> v) {
   cerr << '[ ';
   for (T i : v) {
     _print(i);
     cerr << ' ';
   }
   cerr << ']';
 }
 template <class T>
 void _print(set<T> v) {
   cerr << '[ ';
   for (T i : v) {
     _print(i);
     cerr << ' ';
   }
   cerr << ']';
 }
 template <class T>
 void _print(multiset<T> v) {
   cerr << '[ ';
   for (T i : v) {
     _print(i);
     cerr << ' ';
   }
   cerr << ']';
 }
 template <class T, class V>
 void _print(map<T, V> v) {
   cerr << '[ ';
   for (auto i : v) {
     _print(i);
     cerr << ' ';
   }
   cerr << ']';
 }
 long long int round_up(long long int a, long long int b) {
   return (a + b - 1) / b;
 }
 long long int gcd(long long int a, long long int b) {
   if (b > a) {
     return gcd(b, a);
   }
   if (b == 0) {
     return a;
   }
   return gcd(b, a % b);
 }
 long long int expo(long long int a, long long int b, long long int mod) {
   long long int res = 1;
   while (b > 0) {
     if (b & 1) res = (res * a) % mod;
     a = (a * a) % mod;
     b = b >> 1;
   }
   return res;
 }
 void extendgcd(long long int a, long long int b, long long int *v) {
   if (b == 0) {
     v[0] = 1;
     v[1] = 0;
     v[2] = a;
     return;
   }
   extendgcd(b, a % b, v);
   long long int x = v[1];
   v[1] = v[0] - v[1] * (a / b);
   v[0] = x;
   return;
 }
 long long int mminv(long long int a, long long int b) {
   long long int arr[3];
   extendgcd(a, b, arr);
   return arr[0];
 }
 long long int mminvprime(long long int a, long long int b) {
   return expo(a, b - 2, b);
 }
 bool revsort(long long int a, long long int b) { return a > b; }
 void swap(int &x, int &y) {
   int temp = x;
   x = y;
   y = temp;
 }
 long long int combination(long long int n, long long int r, long long int m,
                           long long int *fact, long long int *ifact) {
   long long int val1 = fact[n];
   long long int val2 = ifact[n - r];
   long long int val3 = ifact[r];
   return (((val1 * val2) % m) * val3) % m;
 }
 void google(int t) { cout << 'Case #' << t << ': '; }
 vector<long long int> sieve(int n) {
   int *arr = new int[n + 1]();
   vector<long long int> vect;
   for (int i = 2; i <= n; i++)
     if (arr[i] == 0) {
       vect.push_back(i);
       for (int j = 2 * i; j <= n; j += i) arr[j] = 1;
     }
   return vect;
 }
 long long int mod_add(long long int a, long long int b, long long int m) {
   a = a % m;
   b = b % m;
   return (((a + b) % m) + m) % m;
 }
 long long int mod_mul(long long int a, long long int b, long long int m) {
   a = a % m;
   b = b % m;
   return (((a * b) % m) + m) % m;
 }
 long long int mod_sub(long long int a, long long int b, long long int m) {
   a = a % m;
   b = b % m;
   return (((a - b) % m) + m) % m;
 }
 long long int mod_div(long long int a, long long int b, long long int m) {
   a = a % m;
   b = b % m;
   return (mod_mul(a, mminvprime(b, m), m) + m) % m;
 }
 void solve() {
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     long long int sum = 0;
     long long int ans = 0;
     int arr[n];
     for (int i = 0; i < n; i++) {
       cin >> arr[i];
       sum += arr[i];
       if (sum < 0) {
         ans -= sum;
         sum = 0;
       }
     }
     cout << sum << '
';
   }
 }
 int main() {
   init();
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   solve();
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int b[100005], p[100005];
 int n, m, a;
 bool check(int x) {
   int temp_a = a;
   for (__typeof((x)) i = (0); i < (x); i += 1) {
     int t = x - 1;
     if (p[i] > b[t - i]) {
       if (temp_a < p[i] - b[t - i]) {
         return false;
       } else {
         temp_a -= p[i] - b[t - i];
       }
     }
   }
   return true;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> m >> a;
   for (__typeof((n)) i = (0); i < (n); i += 1) {
     cin >> b[i];
   }
   for (__typeof((m)) i = (0); i < (m); i += 1) {
     cin >> p[i];
   }
   sort(b, b + n, greater<int>());
   sort(p, p + m);
   int l = 0, h = ((n) < (m) ? (n) : (m)), ans = 0;
   while (l < h) {
     int mid = (l + h + 1) / 2;
     if (check(mid) == true) {
       l = mid;
     } else {
       h = mid - 1;
     }
     ans = l;
   }
   int ans2 = 0;
   for (__typeof((ans)) i = (0); i < (ans); i += 1) {
     ans2 += p[i];
   }
   ans2 -= a;
   if (ans2 < 0) {
     ans2 = 0;
   }
   cout << ans << ' ' << ans2;
 }


#include <bits/stdc++.h>
 using namespace std;
 using ll = long long;
 using vi = vector<long long>;
 using pii = pair<long long, long long>;
 const signed INF_ = 1001001001;
 const long long INF = 1001001001001001001LL;
 const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
                 DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
 template <class T>
 ostream &operator<<(ostream &os, const vector<T> &v) {
   for (auto i = (begin(v)); i != (end(v)); ++i)
     os << *i << (i == end(v) - 1 ? '' : ' ');
   return os;
 }
 template <class T>
 istream &operator>>(istream &is, vector<T> &v) {
   for (auto i = (begin(v)); i != (end(v)); ++i) is >> *i;
   return is;
 }
 template <class T, class U>
 istream &operator>>(istream &is, pair<T, U> &p) {
   is >> p.first >> p.second;
   return is;
 }
 template <class T, class U>
 bool chmax(T &a, const U &b) {
   return a < b ? a = b, 1 : 0;
 }
 template <class T, class U>
 bool chmin(T &a, const U &b) {
   return a > b ? a = b, 1 : 0;
 }
 template <class T>
 void PSUM(T &c) {
   partial_sum(begin(c), end(c), begin(c));
 }
 template <class T>
 using heap = priority_queue<T, vector<T>, greater<T>>;
 struct before_main_function {
   before_main_function() {
     cin.tie(0);
     ios::sync_with_stdio(0);
     cout << setprecision(15) << fixed;
   }
 } before_main_function;
 signed main() {
   long long N, M;
   cin >> N >> M;
   vector<vector<long long>> g(N);
   vector<long long> deg(N);
   for (long long i = 0, ilen = (long long)(M); i < ilen; ++i) {
     long long a, b;
     cin >> a >> b;
     a--, b--;
     g[a].push_back(b);
     g[b].push_back(a);
     deg[a]++;
     deg[b]++;
   }
   vector<pii> ans;
   vector<bool> vis(N);
   {
     queue<long long> q;
     long long st = max_element(begin(deg), end(deg)) - begin(deg);
     q.push(st);
     vis[st] = true;
     while (q.size()) {
       long long v = q.front();
       q.pop();
       for (auto &&w : g[v]) {
         if (vis[w]) continue;
         vis[w] = true;
         ans.push_back({v + 1, w + 1});
         q.push(w);
       }
     }
   }
   for (auto &&p : ans) {
     cout << p.first << ' ' << p.second << endl;
   }
   return (0 ^ 0 ^ 0);
 }


#include <bits/stdc++.h>
 using namespace std;
 int rep[2000005];
 int sz[2000005];
 void dsu(int n) {
   for (int i = 0; i < n; i++) rep[i] = i, sz[i] = 1;
 }
 int findSet(int a) { return ((rep[a] == a) ? a : rep[a] = findSet(rep[a])); }
 int mergeSet(int a, int b) {
   int y = findSet(b);
   int x = findSet(a);
   if (x != y) {
     rep[x] = y;
     sz[y] += sz[x];
     sz[x] = 0;
   }
 }
 long long int grid[1005][1005];
 int vis[1005][1005] = {0};
 vector<pair<long long int, pair<int, int> > > v;
 int n, m;
 long long int k;
 int dx[] = {-1, 0, 0, 1};
 int dy[] = {0, -1, 1, 0};
 int solx = -1;
 int soly = -1;
 long long int sol;
 int cnt;
 void floodfill(int x, int y) {
   if (cnt < 1) return;
   vis[x][y] = 1;
   grid[x][y] = sol;
   cnt--;
   for (int i = 0; i < 4; i++) {
     int newx = x + dx[i];
     int newy = y + dy[i];
     if (newx >= 0 && newx < n && newy >= 0 && newy < m && !vis[newx][newy] &&
         grid[newx][newy] >= sol) {
       floodfill(newx, newy);
     }
   }
 }
 void solve() {
   cin >> n >> m;
   cin >> k;
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       cin >> grid[i][j];
       v.push_back(make_pair(grid[i][j], make_pair(i, j)));
     }
   }
   sort(v.begin(), v.end(), greater<pair<long long int, pair<int, int> > >());
   dsu(n * m);
   int i = 0, j = 0;
   while (i < v.size()) {
     j = i;
     while (j < v.size()) {
       if (v[i].first == v[j].first) {
         int x = v[j].second.first;
         int y = v[j].second.second;
         for (int k = 0; k < 4; k++) {
           int newx = x + dx[k];
           int newy = y + dy[k];
           if (newx >= 0 && newx < n && newy >= 0 && newy < m &&
               grid[newx][newy] >= v[j].first) {
             mergeSet(newx * m + newy, x * m + y);
           }
         }
       } else
         break;
       j++;
     }
     for (int l = i; l < j; l++) {
       if (v[l].first && (k % v[l].first == 0) &&
           sz[findSet(v[l].second.first * m + v[l].second.second)] >=
               k / v[l].first) {
         solx = v[l].second.first;
         soly = v[l].second.second;
         sol = v[l].first;
         cnt = k / v[l].first;
         break;
       }
     }
     i = j;
   }
   if (solx == -1 || soly == -1)
     cout << 'NO
';
   else {
     floodfill(solx, soly);
     cout << 'YES
';
     for (int i = 0; i < n; i++) {
       for (int j = 0; j < m - 1; j++) {
         if (vis[i][j])
           cout << grid[i][j] << ' ';
         else
           cout << '0 ';
       }
       if (vis[i][m - 1])
         cout << grid[i][m - 1] << '
';
       else
         cout << '0
';
     }
   }
 }
 int main() {
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   int k = 0;
   while (1) {
     k++;
     if (k % m == 0) n++;
     n--;
     if (n == 0) break;
   }
   cout << k;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 1e5;
 int edges[MAXN] = {0};
 int main() {
   int n, s;
   cin >> n >> s;
   for (int i = 0; i < n - 1; i++) {
     int a, b;
     cin >> a >> b;
     a--, b--;
     edges[a]++, edges[b]++;
   }
   int amt = 0;
   for (int i = 0; i < n; i++) {
     if (edges[i] == 1) amt++;
   }
   double ans = ((double)s) / ((double)amt);
   cout << fixed << setprecision(10) << ans * 2 << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 int v[100];
 bool cmp(int a, int b) { return a > b; }
 int highest(int n) {
   int i = 1, fact = 0;
   while (2 * i <= n) {
     i *= 2;
     fact++;
   }
   return fact;
 }
 void permute(int left, int right, int pos, int last) {
   if (!pos || (!last && pos == 1)) return;
   int t = highest(pos);
   pos -= (1 << t);
   if (!last) {
     left = right - t - 1;
     swap(v[left + 1], v[right]);
     sort(v + left + 2, v + right, cmp);
     permute(left, right, pos, t);
     return;
   }
   swap(v[left], v[right]);
   --right;
   if (last - t != 1) {
     ++left;
     sort(v + left, v + right + 1);
     permute(left, right, pos + (1 << t), 0);
     return;
   }
   permute(left, right, pos, t);
 }
 int main() {
   int n, m, num = 1, piv = 0, pos, t;
   cin >> n >> m;
   for (int i = 0; i < n; ++i) v[i] = i + 1;
   permute(0, n - 1, m, 0);
   for (int i = 0; i < n; ++i) cout << v[i] << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 class Solver {
  public:
   Solver() = default;
   void read() {
     int n;
     std::cin >> n;
     int left = 0;
     int right = 0;
     for (int i = 0; i < n; ++i) {
       int x, y;
       std::cin >> x >> y;
       if (x < 0)
         left++;
       else
         right++;
     }
     if (left <= 1 || right <= 1)
       std::cout << 'Yes
';
     else
       std::cout << 'No
';
   }
   void solve() {}
   void write() {}
 };
 int main() {
   Solver solver;
   solver.read();
   solver.solve();
   solver.write();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int flow, n, m, S, T, tot, x, y;
 int g[3000], dis[3000], pre[3000];
 int Next[50000], adj[50000], f[50000], c[50000];
 bool used[3000];
 queue<int> q;
 int ans = 0;
 int calc[110][30];
 string t, s;
 void add(int x, int y, int flow, int cost) {
   adj[tot] = y;
   c[tot] = cost;
   f[tot] = flow;
   Next[tot] = g[x];
   g[x] = tot++;
   adj[tot] = x;
   c[tot] = -cost;
   f[tot] = 0;
   Next[tot] = g[y];
   g[y] = tot++;
 }
 bool spfa() {
   for (int i = 1; i <= T; i++) dis[i] = 1 << 23;
   dis[S] = 0;
   memset(used, 0, sizeof(used));
   while (!q.empty()) q.pop();
   q.push(S);
   used[S] = true;
   int p;
   while (!q.empty()) {
     x = q.front();
     used[x] = false;
     p = g[x];
     while (p != -1) {
       y = adj[p];
       if (f[p] && dis[x] + c[p] < dis[y]) {
         dis[y] = dis[x] + c[p];
         pre[y] = p;
         if (!used[y]) {
           used[y] = true;
           q.push(y);
         }
       }
       p = Next[p];
     }
     q.pop();
   }
   return dis[T] != 1 << 23;
 }
 int path() {
   int del = 1 << 23;
   int p = T;
   while (p != S) {
     if (f[pre[p]] < del) del = f[pre[p]];
     p = adj[pre[p] ^ 1];
   }
   p = T;
   while (p != S) {
     f[pre[p]] -= del;
     f[pre[p] ^ 1] += del;
     p = adj[pre[p] ^ 1];
   }
   flow += del;
   return del * dis[T];
 }
 void solve() {
   memset(pre, 0, sizeof(pre));
   while (spfa()) ans += path();
   if (flow != t.size())
     cout << -1 << endl;
   else
     cout << ans << endl;
 }
 int main() {
   cin >> t;
   cin >> n;
   memset(g, -1, sizeof(g));
   tot = 0;
   S = 26 * (n + 1) + n + 1;
   T = S + 1;
   for (int i = 1; i <= n; i++) {
     cin >> s >> x;
     for (int j = 0; j < s.size(); j++) calc[i][s[j] - 'a']++;
     add(S, 26 * (n + 1) + i, x, 0);
   }
   for (int i = 0; i < t.size(); i++) calc[n + 1][t[i] - 'a']++;
   for (int i = 0; i < 26; i++) add(26 * n + i + 1, T, calc[n + 1][i], 0);
   for (int i = 1; i <= n; i++)
     for (int j = 0; j < 26; j++) {
       add(26 * (n + 1) + i, (i - 1) * 26 + j + 1, 1 << 23, 0);
       add((i - 1) * 26 + j + 1, 26 * n + j + 1, calc[i][j], i);
     }
   solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   clock_t clk_begin = clock();
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n;
   cin >> n;
   string s;
   cin >> s;
   int c = 0;
   for (int i = 0; i < n; ++i)
     if (s[i] == '8') c++;
   n /= 11;
   cout << min(n, c) << '
';
   ;
   cerr << 'Time taken: ' << (double)(clock() - clk_begin) / CLOCKS_PER_SEC
        << '
';
   return 0;
   ;
 }


#include <bits/stdc++.h>
 using namespace std;
 int form[9], piece[9][2];
 map<int, int> res;
 string result[] = {
     'illegal', 'the first player won', 'the second player won', 'draw', 'first',
     'second'};
 void pre() {
   form[0] = 3;
   piece[0][0] = 1;
   piece[0][1] = 1 << 1;
   for (int i = 1; i < 9; i++) {
     form[i] = form[i - 1] << 2;
     piece[i][0] = piece[i - 1][0] << 2;
     piece[i][1] = piece[i - 1][1] << 2;
   }
 }
 int add(int now, int p, int who, bool end) {
   int px = p / 3 * 3, py = p % 3;
   if ((piece[px][who] & now) && (piece[px + 1][who] & now) &&
       (piece[px + 2][who] & now))
     return who + 1;
   if ((piece[py][who] & now) && (piece[py + 3][who] & now) &&
       (piece[py + 6][who] & now))
     return who + 1;
   if ((p == 0 || p == 4 || p == 8) && (piece[0][who] & now) &&
       (piece[4][who] & now) && (piece[8][who] & now))
     return who + 1;
   if ((p == 2 || p == 4 || p == 6) && (piece[2][who] & now) &&
       (piece[4][who] & now) && (piece[6][who] & now))
     return who + 1;
   return end ? 3 : 5 - who;
 }
 void enumeration(int now, int who, int round) {
   for (int p = 0; p < 9; p++) {
     if (now & form[p]) continue;
     now ^= piece[p][who];
     if (!res[now]) {
       res[now] = add(now, p, who, round == 9);
       if (res[now] > 3) enumeration(now, who ^ 1, round + 1);
     }
     now ^= piece[p][who];
   }
 }
 int main() {
   int now = 0;
   char ch;
   pre();
   enumeration(now, 0, 1);
   for (int p = 0; p < 9; p++) {
     cin >> ch;
     if (ch == 'X')
       now ^= piece[p][0];
     else if (ch == '0')
       now ^= piece[p][1];
   }
   cout << result[res[now]] << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int parent(int x, vector<int> &v) {
   return v[x] = (v[x] == x ? x : parent(v[x], v));
 }
 void u(int x, int y, vector<int> &v, vector<int> &r) {
   x = parent(x, v);
   y = parent(y, v);
   if (r[x] == r[y]) {
     if (x == y) {
       return;
     }
     r[x]++;
   }
   if (r[x] > r[y]) {
     v[y] = x;
   } else {
     v[x] = y;
   }
   return;
 }
 int main() {
   int n;
   scanf('%d
', &n);
   vector<int> v(26, -1);
   vector<int> r(26);
   string s;
   for (int i = 0; i < n; i++) {
     cin >> s;
     if (v[s[0] - 'a'] == -1) {
       v[s[0] - 'a'] = s[0] - 'a';
     }
     for (int i = 1; i < (int)s.size(); i++) {
       if (v[s[i] - 'a'] == -1) {
         v[s[i] - 'a'] = s[i] - 'a';
       }
       u(s[i] - 'a', s[0] - 'a', v, r);
     }
   }
   unordered_map<int, int> h;
   for (int i = 0; i < 26; i++) {
     if (v[i] != -1) {
       h[parent(v[i], v)] = 1;
     }
   }
   cout << h.size();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int a[105], b[105];
 int main() {
   int n, i, j, k, x, y;
   while (~scanf('%d', &n)) {
     for (i = 1; i <= n; i++) {
       scanf('%d', &a[i]);
     }
     scanf('%d%d', &x, &y);
     for (i = 1, j = 0; i <= n; i++) {
       int count1 = 0, count2 = 0;
       for (k = 1; k <= n; k++) {
         if (k >= i)
           count1 += a[k];
         else
           count2 += a[k];
       }
       if ((count1 >= x && count1 <= y) && (count2 >= x && count2 <= y)) {
         b[j++] = i;
         break;
       }
     }
     if (j == 0) {
       printf('0
');
       continue;
     }
     printf('%d
', b[0]);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   vector<vector<int>> a(n, vector<int>(m, 0));
   vector<vector<int>> b(n, vector<int>(m, 0));
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       cin >> a[i][j];
     }
   }
   vector<pair<int, int>> ans;
   for (int i = 0; i < n - 1; i++) {
     for (int j = 0; j < m - 1; j++) {
       if (a[i][j] == 1 && a[i][j + 1] == 1 && a[i + 1][j + 1] == 1 &&
           a[i + 1][j] == 1) {
         ans.push_back(make_pair(i, j));
         b[i][j] = 1;
         b[i][j + 1] = 1;
         b[i + 1][j + 1] = 1;
         b[i + 1][j] = 1;
       }
     }
   }
   bool flag = true;
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       if (a[i][j] != b[i][j]) {
         flag = false;
         i = n;
         break;
       }
     }
   }
   if (flag) {
     cout << ans.size() << endl;
     for (auto it = ans.begin(); it != ans.end(); it++) {
       cout << (*it).first + 1 << ' ' << (*it).second + 1 << endl;
     }
   } else
     cout << -1 << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T, typename U>
 inline void smin(T &a, const U &b) {
   if (a > b) a = b;
 }
 template <typename T, typename U>
 inline void smax(T &a, const U &b) {
   if (a < b) a = b;
 }
 template <class T>
 inline void gn(T &first) {
   char c, sg = 0;
   while (c = getchar(), (c > '9' || c < '0') && c != '-')
     ;
   for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
        c = getchar())
     first = (first << 1) + (first << 3) + c - '0';
   if (sg) first = -first;
 }
 template <class T1, class T2>
 inline void gn(T1 &x1, T2 &x2) {
   gn(x1), gn(x2);
 }
 int power(int a, int b, int m, int ans = 1) {
   for (; b; b >>= 1, a = 1LL * a * a % m)
     if (b & 1) ans = 1LL * ans * a % m;
   return ans;
 }
 vector<int> adj[500500];
 int can0[500500], can1[500500], dp0[500500], dp1[500500];
 int dp[4];
 int n, sz[500500];
 void dfs(int u, int f = 0) {
   sz[u] = 1;
   for (int v : adj[u])
     if (v != f) dfs(v, u), sz[u] += sz[v];
   can0[u] = 1;
   dp[0] = 1, dp[1] = 0;
   for (int v : adj[u]) {
     if (v == f) continue;
     can0[u] &= can1[v];
     int t[2] = {dp[0], dp[1]};
     dp[0] = t[0] * can1[v];
     dp[1] = t[0] * can0[v] + t[1] * can1[v];
   }
   can1[u] = dp[1];
   dp[0] = 1, dp[1] = 0;
   for (int v : adj[u]) {
     if (v == f) continue;
     int t[2] = {dp[0], dp[1]};
     dp[0] = t[0] * can1[v];
     dp[1] = t[1] * can1[v] + t[0] * dp1[v];
   }
   dp0[u] = dp[1];
   dp1[u] = can0[u];
   dp[0] = 1, dp[1] = 0;
   for (int v : adj[u]) {
     if (v == f) continue;
     int t[2] = {dp[0], dp[1]};
     dp[0] = t[0] * can1[v];
     dp[1] = t[1] * can1[v] + t[0] * dp0[v];
   }
   dp1[u] += dp[1];
   dp[0] = 1, dp[1] = dp[2] = dp[3] = 0;
   for (int v : adj[u]) {
     if (v == f) continue;
     int t[4] = {dp[0], dp[1], dp[2], dp[3]};
     dp[0] = t[0] * can1[v];
     dp[1] = t[0] * can0[v] + t[1] * can1[v];
     dp[2] = t[0] * dp1[v] + t[2] * can1[v];
     dp[3] = t[1] * dp1[v] + t[2] * can0[v] + t[3] * can1[v];
   }
   dp1[u] += dp[3];
 }
 long long ans;
 int c0[500500], c1[500500], d0[500500], d1[500500];
 int R[500500], Rdp[500500][4];
 void redfs(int u, int f = 0) {
   if (f) {
     if (can1[u] && c1[u])
       ans += (long long)sz[u] * (n - sz[u]);
     else
       ans += (long long)dp1[u] * d1[u];
   }
   R[adj[u].size()] = 1;
   Rdp[adj[u].size()][0] = 1;
   Rdp[adj[u].size()][1] = 0;
   for (int i = adj[u].size(); i--;) {
     int v = adj[u][i];
     R[i] = R[i + 1] & (v == f ? c1[u] : can1[v]);
     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]);
     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? c0[u] : can0[v]) +
                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]);
   }
   int cur = 1;
   dp[0] = 1, dp[1] = 0;
   for (int i = 0; i < adj[u].size(); i++) {
     int v = adj[u][i];
     if (v != f) {
       c0[v] = cur & R[i + 1];
       c1[v] = dp[0] & Rdp[i + 1][1] | dp[1] & Rdp[i + 1][0];
     }
     int t[2] = {dp[0], dp[1]};
     cur &= (v == f ? c1[u] : can1[v]);
     dp[0] = t[0] * (v == f ? c1[u] : can1[v]);
     dp[1] =
         t[0] * (v == f ? c0[u] : can0[v]) + t[1] * (v == f ? c1[u] : can1[v]);
   }
   Rdp[adj[u].size()][0] = 1;
   Rdp[adj[u].size()][1] = 0;
   for (int i = adj[u].size(); i--;) {
     int v = adj[u][i];
     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]);
     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? d1[u] : dp1[v]) +
                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]);
   }
   dp[0] = 1, dp[1] = 0;
   for (int i = 0; i < adj[u].size(); i++) {
     int v = adj[u][i];
     if (v != f) d0[v] = dp[0] * Rdp[i + 1][1] + dp[1] * Rdp[i + 1][0];
     int t[2] = {dp[0], dp[1]};
     dp[0] = t[0] * (v == f ? c1[u] : can1[v]);
     dp[1] =
         t[1] * (v == f ? c1[u] : can1[v]) + t[0] * (v == f ? d1[u] : dp1[v]);
   }
   for (int v : adj[u])
     if (v != f) d1[v] = c0[v];
   Rdp[adj[u].size()][0] = 1;
   Rdp[adj[u].size()][1] = 0;
   for (int i = adj[u].size(); i--;) {
     int v = adj[u][i];
     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]);
     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? d0[u] : dp0[v]) +
                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]);
   }
   dp[0] = 1, dp[1] = 0;
   for (int i = 0; i < adj[u].size(); i++) {
     int v = adj[u][i];
     if (v != f) d1[v] += dp[0] * Rdp[i + 1][1] + dp[1] * Rdp[i + 1][0];
     int t[2] = {dp[0], dp[1]};
     dp[0] = t[0] * (v == f ? c1[u] : can1[v]);
     dp[1] =
         t[1] * (v == f ? c1[u] : can1[v]) + t[0] * (v == f ? d0[u] : dp0[v]);
   }
   Rdp[adj[u].size()][0] = 1;
   Rdp[adj[u].size()][1] = 0;
   Rdp[adj[u].size()][2] = 0;
   Rdp[adj[u].size()][3] = 0;
   for (int i = adj[u].size(); i--;) {
     int v = adj[u][i];
     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]);
     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? c0[u] : can0[v]) +
                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]);
     Rdp[i][2] = Rdp[i + 1][0] * (v == f ? d1[u] : dp1[v]) +
                 Rdp[i + 1][2] * (v == f ? c1[u] : can1[v]);
     Rdp[i][3] = Rdp[i + 1][1] * (v == f ? d1[u] : dp1[v]) +
                 Rdp[i + 1][2] * (v == f ? c0[u] : can0[v]) +
                 Rdp[i + 1][3] * (v == f ? c1[u] : can1[v]);
   }
   dp[0] = 1, dp[1] = dp[2] = dp[3] = 0;
   for (int i = 0; i < adj[u].size(); i++) {
     int v = adj[u][i];
     if (v != f)
       d1[v] += dp[0] * Rdp[i + 1][3] + dp[1] * Rdp[i + 1][2] +
                dp[2] * Rdp[i + 1][1] + dp[3] * Rdp[i + 1][0];
     int t[4] = {dp[0], dp[1], dp[2], dp[3]};
     dp[0] = t[0] * (v == f ? c1[u] : can1[v]);
     dp[1] =
         t[0] * (v == f ? c0[u] : can0[v]) + t[1] * (v == f ? c1[u] : can1[v]);
     dp[2] =
         t[0] * (v == f ? d1[u] : dp1[v]) + t[2] * (v == f ? c1[u] : can1[v]);
     dp[3] = t[1] * (v == f ? d1[u] : dp1[v]) +
             t[2] * (v == f ? c0[u] : can0[v]) +
             t[3] * (v == f ? c1[u] : can1[v]);
   }
   for (int v : adj[u])
     if (v != f) redfs(v, u);
 }
 int main() {
   cin >> n;
   for (int i = 1; i < n; i++) {
     int u, v;
     gn(u, v);
     adj[u].push_back(v);
     adj[v].push_back(u);
   }
   dfs(1);
   redfs(1);
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 using pii = pair<int, int>;

 void solve() {
 	int m, k;
 	cin >> m >> k;

 	vector<pii> a;

 	int ai;
 	for (int i = 1; i <= k; i++) {
 		cin >> ai;
 		a.push_back({ai, i});
 	}

 	sort(a.begin(), a.end(), greater<pii>());

 	int n = 1;
 	for (; ; n++) {
 		if (m > n * n - (n / 2) * (n / 2)) continue;
 		if (a[0].first > n * ((n + 1) / 2)) continue;
 		break;
 	}

 	vector<pii> pos[3];
 	for (int i = 0; i < n; i++) {
 		for (int j = 0; j < n; j++) {
 			int idx = ((i & 1) << 1) + (j & 1);
 			if (idx != 3)
 				pos[idx].push_back({i, j});
 		}
 	}

 	vector<vector<int>> ans(n, vector<int>(n, 0));

 	for (auto [cnt, val] : a) {
 		vector<pii> &curr = pos[1].empty() ? pos[2] : pos[1];

 		for (; !curr.empty() && cnt > 0; cnt--) {
 			auto [x, y] = curr.back();
 			curr.pop_back();
 			ans[x][y] = val;
 		}
 		for (; cnt > 0; cnt--) {
 			auto [x, y] = pos[0].back();
 			pos[0].pop_back();
 			ans[x][y] = val;
 		}
 	}

 	cout << n << '
';
 	for (int i = 0; i < n; i++) {
 		for (int j = 0; j < n; j++) {
 			cout << ans[i][j] << ' ';
 		}
 		cout << '
';
 	}
 }

 int main() {
 	ios_base::sync_with_stdio(0);
 	cin.tie(0);

 	int t;
 	cin >> t;

 	while (t--) {
 		solve();
 	}

 	return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 211, M = N * N / 2;
 int u[M], v[M], w[M];
 int f[N][N];
 bool check(int n, int m, int lim) {
   for (int i = 1; i <= m; i++) {
     vector<pair<int, int>> v;
     bool t = 1;
     for (int j = 1; j <= n; j++) {
       int l = f[j][u[i]], r = f[j][::v[i]];
       if (lim >= l && lim >= r) {
         if (2 * lim - l - r >= w[i]) {
           v.push_back({0, 1});
           v.push_back({w[i] + 1, -1});
         } else {
           v.push_back({0, 1});
           v.push_back({min(w[i], lim - l) + 1, -1});
           v.push_back({w[i] - min(w[i], lim - r), 1});
           v.push_back({w[i] + 1, -1});
         }
       } else if (lim >= l) {
         v.push_back({0, 1});
         v.push_back({min(w[i], lim - l) + 1, -1});
       } else if (lim >= r) {
         v.push_back({w[i] - min(w[i], lim - r), 1});
         v.push_back({w[i] + 1, -1});
       } else {
         t = 0;
         break;
       }
     }
     if (!t) continue;
     sort(v.begin(), v.end());
     int cnt = 0;
     for (auto e : v) {
       cnt += e.second;
       if (cnt == n) return 1;
     }
   }
   return 0;
 }
 int main() {
   ios ::sync_with_stdio(0);
   int n, m;
   cin >> n >> m;
   memset(f, 0x3f, sizeof f);
   for (int i = 1; i <= n; i++) f[i][i] = 0;
   for (int i = 1; i <= m; i++) {
     cin >> u[i] >> v[i] >> w[i];
     w[i] *= 2;
     f[u[i]][v[i]] = f[v[i]][u[i]] = w[i];
   }
   for (int k = 1; k <= n; k++)
     for (int i = 1; i <= n; i++)
       for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
   int l = 0, r = 1e8L;
   while (l + 1 < r) {
     int im = (l + r) / 2;
     if (check(n, m, im - 1))
       r = im;
     else
       l = im;
   }
   cout << fixed << setprecision(1);
   cout << l / 2.0 << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int ans1, ans2;
 int t[4], v[4][10], T[10][10], A, B, C;
 map<string, int> mymap;
 const char *help[] = {'Anka',   'Chapay', 'Cleo',       'Troll',
                       'Dracul', 'Snowy',  'Hexadecimal'};
 int countT() {
   int ret = 0;
   for (int k = 0; k < 3; k++) {
     for (int i = 0; i < t[k]; i++)
       for (int j = 0; j < t[k]; j++)
         if (T[v[k][i]][v[k][j]]) ret++;
   }
   return ret;
 }
 void go(int x) {
   if (x == 8) {
     if (!t[0] || !t[1] || !t[2]) return;
     int h[4];
     h[0] = A / t[0];
     h[1] = B / t[1];
     h[2] = C / t[2];
     sort(h, h + 3);
     int d = h[2] - h[0];
     int p = countT();
     if (d < ans1) {
       ans1 = d;
       ans2 = p;
     } else if (d == ans1) {
       ans2 = max(ans2, p);
     }
     return;
   }
   for (int i = 0; i < 3; i++) {
     v[i][t[i]++] = x;
     go(x + 1);
     t[i]--;
   }
   return;
 }
 int main() {
   char s1[30], s2[30], s3[30];
   for (int i = 0; i < 7; i++) mymap[help[i]] = i + 1;
   memset(T, 0, sizeof(T));
   int m;
   cin >> m;
   while (m--) {
     cin >> s1 >> s2 >> s3;
     int u, v;
     u = mymap[s1];
     v = mymap[s3];
     T[u][v] = 1;
   }
   cin >> A >> B >> C;
   ans1 = 2000000001;
   ans2 = 0;
   memset(t, 0, sizeof(t));
   go(1);
   cout << ans1 << ' ' << ans2 << endl;
   return 0;
 }


#include <bits/stdc++.h>
 typedef long long ll ;
 using namespace std;
 const int maxn=2e5+5;
 const int maxm=5e6+5;
 #define P pair<int,int>
 #define fi first
 #define se second
 int t,n;
 int a[maxn];
 int vis[maxm*2];
 P mp[maxm];

 int main() {
     ios::sync_with_stdio(false);
     cin.tie(nullptr), cout.tie(nullptr);
     cin>>n;
     for(int i=1;i<=n;i++){
         cin>>a[i];
     }
     int sum1;
     for(int i=1;i<=n;i++){
         for(int j=i+1;j<=n;j++){
             sum1=a[i]+a[j];
             if(mp[sum1].fi==0 &&mp[sum1].se==0){
                 mp[sum1]={i,j};
             }else if(i!=mp[sum1].fi&&i!=mp[sum1].se&&j!=mp[sum1].fi&&j!=mp[sum1].se){
                 cout<<'YES'<<endl;
                 cout<<i<<' '<<j<<' '<<mp[sum1].fi<<' '<<mp[sum1].se<<endl;
                 return 0;
             }
         }
     }
     cout<<'NO'<<endl;
     return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 T BigMod(T b, T p, T m) {
   if (p == 0) return 1;
   if (p % 2 == 0) {
     T s = BigMod(b, p / 2, m);
     return ((s % m) * (s % m)) % m;
   }
   return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
 }
 template <typename T>
 T ModInv(T b, T m) {
   return BigMod(b, m - 2, m);
 }
 template <typename T>
 T in() {
   char ch;
   T n = 0;
   bool ng = false;
   while (1) {
     ch = getchar();
     if (ch == '-') {
       ng = true;
       ch = getchar();
       break;
     }
     if (ch >= '0' && ch <= '9') break;
   }
   while (1) {
     n = n * 10 + (ch - '0');
     ch = getchar();
     if (ch < '0' || ch > '9') break;
   }
   return (ng ? -n : n);
 }
 template <typename T>
 T POW(T B, T printf) {
   if (printf == 0) return 1;
   if (printf & 1)
     return B * POW(B, printf - 1);
   else
     return (POW(B, printf / 2) * POW(B, printf / 2));
 }
 template <typename T>
 T Bigmod(T b, T p, T m) {
   if (p == 0)
     return 1;
   else if (!(p & 1))
     return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
   else
     return ((b % m) * Bigmod(b, p - 1, m)) % m;
 }
 template <typename T>
 T Dis(T x1, T y1, T x2, T y2) {
   return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
 }
 template <typename T>
 T Angle(T x1, T y1, T x2, T y2) {
   return atan(double(y1 - y2) / double(x1 - x2));
 }
 template <typename T>
 T DIFF(T a, T b) {
   T d = a - b;
   if (d < 0)
     return -d;
   else
     return d;
 }
 template <typename T>
 T ABS(T a) {
   if (a < 0)
     return -a;
   else
     return a;
 }
 template <typename T>
 T gcd(T a, T b) {
   if (a < 0) return gcd(-a, b);
   if (b < 0) return gcd(a, -b);
   return (b == 0) ? a : gcd(b, a % b);
 }
 template <typename T>
 T lcm(T a, T b) {
   if (a < 0) return lcm(-a, b);
   if (b < 0) return lcm(a, -b);
   return a * (b / gcd(a, b));
 }
 template <typename T>
 T euclide(T a, T b, T &x, T &y) {
   if (a < 0) {
     T d = euclide(-a, b, x, y);
     x = -x;
     return d;
   }
   if (b < 0) {
     T d = euclide(a, -b, x, y);
     y = -y;
     return d;
   }
   if (b == 0) {
     x = 1;
     y = 0;
     return a;
   } else {
     T d = euclide(b, a % b, x, y);
     T t = x;
     x = y;
     y = t - (a / b) * y;
     return d;
   }
 }
 template <typename T>
 void ia(T a[], int n) {
   for (int i = 0; i < n; i++) cin >> a[i];
 }
 template <typename T>
 void pa(T a[], int n) {
   for (int i = 0; i < n - 1; i++) cout << a[i] << ' ';
   cout << a[n - 1] << endl;
 }
 template <typename T>
 long long int isLeft(T a, T b, T c) {
   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
 }
 int Set(int N, int pos) { return N = N | (1 << pos); }
 int Reset(int N, int pos) { return N = N & ~(1 << pos); }
 bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
 template <class T, class first>
 inline T togglebit(T a, first i) {
   T t = 1;
   return (a ^ (t << i));
 }
 int toInt(string s) {
   int sm;
   stringstream ss(s);
   ss >> sm;
   return sm;
 }
 int toLlint(string s) {
   long long int sm;
   stringstream ss(s);
   ss >> sm;
   return sm;
 }
 int cdigittoint(char ch) { return ch - '0'; }
 bool isVowel(char ch) {
   ch = toupper(ch);
   if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
     return true;
   return false;
 }
 bool isConst(char ch) {
   if (isalpha(ch) && !isVowel(ch)) return true;
   return false;
 }
 double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); }
 double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); }
 vector<int> adj[200007], dag[200007], par[200007];
 int DP[200007][18], siz[200007];
 long long int dis[200007];
 int L[200007];
 int inDeg[200007];
 bool vis[200007];
 void Init(int n) {
   for (int i = 0; i <= n; i++) {
     dis[i] = 1LL << 60;
     adj[i].clear();
     dag[i].clear();
     par[i].clear();
     inDeg[i] = siz[i] = vis[i] = 0;
     for (int j = 0; j < 18; j++) DP[i][j] = -1;
   }
 }
 void Update(int u, int p) {
   DP[u][0] = p;
   L[u] = L[p] + 1;
   adj[p].push_back(u);
   for (int i = 1; i < 18; i++)
     if (DP[u][i - 1] != -1) DP[u][i] = DP[DP[u][i - 1]][i - 1];
 }
 int LCA(int p, int q) {
   if (L[p] < L[q]) swap(p, q);
   for (int i = 18 - 1; i >= 0; i--)
     if (L[p] - (1 << i) >= L[q]) p = DP[p][i];
   if (p == q) return p;
   for (int i = 18 - 1; i >= 0; i--)
     if (DP[p][i] != -1 && DP[p][i] != DP[q][i]) {
       p = DP[p][i];
       q = DP[q][i];
     }
   return DP[p][0];
 }
 void Dijkstra(int n, int s) {
   set<pair<long long int, long long int> > pq;
   pq.insert(make_pair(0, s));
   dis[s] = 0;
   L[s] = 1;
   while (!pq.empty()) {
     pair<long long int, long long int> tmp = *pq.begin();
     pq.erase(pq.begin());
     int u = tmp.second;
     for (int i = 0; i < adj[u].size(); i += 2) {
       int v = adj[u][i];
       int w = adj[u][i + 1];
       if (dis[v] > dis[u] + w) {
         dis[v] = dis[u] + w;
         pq.insert(make_pair(dis[v], v));
       }
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = 0; j < adj[i].size(); j += 2) {
       int v = adj[i][j];
       int w = adj[i][j + 1];
       if (dis[i] + w == dis[v]) {
         inDeg[v]++;
         dag[i].push_back(v);
         par[v].push_back(i);
       }
     }
     adj[i].clear();
   }
 }
 void TopSort(int s) {
   queue<int> q;
   q.push(s);
   L[s] = 1;
   while (!q.empty()) {
     int u = q.front();
     q.pop();
     if (!par[u].empty()) {
       int lca = par[u][0];
       for (int i = 1; i < par[u].size(); i++) {
         lca = LCA(lca, par[u][i]);
       }
       Update(u, lca);
     }
     for (int i = 0; i < dag[u].size(); i++) {
       int v = dag[u][i];
       inDeg[v]--;
       if (!inDeg[v]) q.push(v);
     }
   }
 }
 void Dfs(int u) {
   siz[u] = 1;
   vis[u] = 1;
   for (int i = 0; i < adj[u].size(); i++) {
     Dfs(adj[u][i]);
     siz[u] += siz[adj[u][i]];
   }
 }
 int main() {
   int n, m, s;
   scanf('%d%d%d', &n, &m, &s);
   Init(n);
   for (int i = 1; i <= m; i++) {
     int u, v, w;
     scanf('%d%d%d', &u, &v, &w);
     adj[u].push_back(v);
     adj[u].push_back(w);
     adj[v].push_back(u);
     adj[v].push_back(w);
   }
   Dijkstra(n, s);
   TopSort(s);
   Dfs(s);
   int mx = 0;
   for (int i = 1; i <= n; i++) {
     if (i != s) mx = max(mx, siz[i]);
   }
   cout << mx << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int M = 2048;
 char s[M], b[M], e[M], fb[M], fe[M];
 int r[2][M], *p = r[0], *q = r[1], h[M];
 int main(void) {
   cin >> s >> b >> e;
   int ns = strlen(s), nb = strlen(b), ne = strlen(e), nn = nb > ne ? nb : ne,
       w = 0;
   for (int i = ns; i-- > 0;) {
     if (!strncmp(s + i, b, nb)) fb[i] = 1;
     if (!strncmp(s + i, e, ne)) fe[i] = 1;
   }
   for (int i = 0, *t; i < ns; ++i, t = p, p = q, q = t)
     for (int j = i; j-- > 0;) {
       q[j + 1] = (s[i] == s[j]) ? (p[j] + 1) : 0;
       if (h[i] < q[j + 1]) h[i] = q[j + 1];
     }
   for (int i = ns; i-- > 0;)
     if (fb[i])
       for (int j = i + nn; j <= ns; ++j)
         if (fe[j - ne] && h[j - 1] < j - i) ++w;
   cout << w << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int mod = 1e9 + 7;
 int inv[25];
 long long a[25];
 int power(int a, int b) {
   int c = 1;
   for (; b; b >>= 1) {
     if (b & 1) c = (long long)c * a % mod;
     a = (long long)a * a % mod;
   }
   return c;
 }
 int C(long long y, int x) {
   if (y < 0 || x < 0 || y < x) return 0;
   y %= mod;
   if (y == 0 || x == 0) return 1;
   int ans = 1;
   for (int i = 0; i < x; i++) ans = (long long)ans * (y - i) % mod;
   for (int i = 1; i <= x; i++) ans = (long long)ans * inv[i] % mod;
   return ans;
 }
 int main() {
   int n;
   long long m;
   long long ans = 0;
   for (int i = 1; i <= 20; i++) inv[i] = power(i, mod - 2);
   cin >> n >> m;
   for (int i = 1; i <= n; i++) cin >> a[i];
   for (int x = 0; x < (1 << n); x++) {
     if (x == 0)
       ans = (ans + C(n + m - 1, n - 1)) % mod;
     else {
       long long t = n + m;
       int p = 0;
       for (int i = 0; i < n; i++)
         if ((x >> i) & 1) {
           p++;
           t -= a[i + 1];
         }
       t -= (p + 1);
       if (p & 1)
         ans = (ans - C(t, n - 1)) % mod;
       else
         ans = (ans + C(t, n - 1)) % mod;
     }
   }
   cout << (ans + mod) % mod << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T1, typename T2>
 string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
   bool done_something = false;
   stringstream res;
   res << '[';
   for (; begin_iter != end_iter and counter; ++begin_iter) {
     done_something = true;
     counter--;
     res << *begin_iter << ', ';
   }
   string str = res.str();
   if (done_something) {
     str.pop_back();
     str.pop_back();
   }
   str += ']';
   return str;
 }
 vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
   vector<int> ord(size);
   for (int i = 0; i < size; i++) ord[i] = i;
   sort(ord.begin(), ord.end(), compare);
   return ord;
 }
 template <typename T>
 bool MinPlace(T& a, const T& b) {
   if (a > b) {
     a = b;
     return true;
   }
   return false;
 }
 template <typename T>
 bool MaxPlace(T& a, const T& b) {
   if (a < b) {
     a = b;
     return true;
   }
   return false;
 }
 const int MAXN = 100100;
 int a[MAXN];
 int pr[MAXN];
 int last_apparizione[MAXN];
 int N;
 int res[MAXN];
 int EasyCalc(int k) {
   if (res[k]) return res[k];
   int ris = 1;
   int curr_start = 0;
   int curr_size = 1;
   for (int i = 1; i < N; i++) {
     curr_size += curr_start > pr[i];
     if (curr_size > k) {
       curr_size = 1;
       curr_start = i;
       ris++;
     }
   }
   return ris;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin >> N;
   for (int i = 0; i < N; i++) cin >> a[i];
   for (int i = 0; i < MAXN; i++) last_apparizione[i] = -1;
   for (int i = 0; i < N; i++) {
     pr[i] = last_apparizione[a[i]];
     last_apparizione[a[i]] = i;
   }
   int rad = min((int)(2 * sqrt(N)), N);
   for (int k = 1; k <= rad; k++) res[k] = EasyCalc(k);
   int curr = rad;
   while (curr <= N) {
     int l = curr + 1;
     int r = N + 1;
     while (l < r) {
       int m = (l + r) / 2;
       res[m] = EasyCalc(m);
       if (res[m] == res[curr])
         l = m + 1;
       else
         r = m;
     }
     int prox = l;
     for (int i = curr + 1; i < prox; i++) res[i] = res[curr];
     curr = prox;
   }
   for (int k = 1; k <= N; k++) cout << res[k] << ' ';
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, ans = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
     int x, y, z;
     cin >> x >> y >> z;
     ans += (x + y + z >= 2);
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char str[1010];
 int pri[510][510];
 int pre[510][510];
 int f[510][510];
 bool v[510] = {0};
 int N;
 void init() {
   gets(str);
   scanf('%d', &N);
 }
 void prework() {
   int len = strlen(str);
   for (int i = 0; i < len; ++i)
     for (int j = 0; j < len; ++j) {
       int p = 0;
       int tmp = 0;
       while (i + p < j - p) {
         if (str[i + p] != str[j - p]) tmp++;
         p++;
       }
       pri[i + 1][j + 1] = tmp;
     }
 }
 void dp() {
   int len = strlen(str);
   memset(f, 10, sizeof(f));
   f[0][0] = 0;
   for (int i = 1; i <= len; ++i) {
     for (int j = 1; j <= N; ++j)
       for (int k = 0; k < i; ++k) {
         if (f[i][j] > f[k][j - 1] + pri[k + 1][i]) {
           f[i][j] = f[k][j - 1] + pri[k + 1][i];
           pre[i][j] = k;
         }
       }
   }
   int Ans = 1000000000;
   int p;
   for (int i = 1; i <= N; ++i)
     if (f[len][i] < Ans) {
       Ans = f[len][i];
       p = i;
     }
   printf('%d
', Ans);
   int q = len;
   while (q != 0) {
     v[pre[q][p]] = 1;
     int x = 0;
     while (pre[q][p] + x < q - 1 - x) {
       str[pre[q][p] + x] = str[q - 1 - x];
       x++;
     }
     q = pre[q][p];
     p--;
   }
   v[0] = 0;
   for (int i = 0; i < len; ++i) {
     if (v[i]) putchar('+');
     putchar(str[i]);
   }
 }
 int main() {
   init();
   prework();
   dp();
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('Ofast')
 #pragma GCC optimize('unroll-loops')
 #pragma GCC target( \n     'sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native')
 using namespace std;
 template <typename T1, typename T2>
 void ckmin(T1 &a, T2 b) {
   if (a > b) a = b;
 }
 template <typename T1, typename T2>
 void ckmax(T1 &a, T2 b) {
   if (a < b) a = b;
 }
 int read() {
   int x = 0, f = 0;
   char ch = getchar();
   while (!isdigit(ch)) f |= ch == '-', ch = getchar();
   while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
   return f ? -x : x;
 }
 template <typename T>
 void print(T x) {
   if (x < 0) putchar('-'), x = -x;
   if (x >= 10) print(x / 10);
   putchar(x % 10 + '0');
 }
 template <typename T>
 void print(T x, char let) {
   print(x), putchar(let);
 }
 const int mod = 998244353;
 const int G = 3, Gi = 332748118;
 int add(int x, int y) {
   if ((x += y) >= mod) x -= mod;
   return x;
 }
 int sub(int x, int y) {
   if ((x -= y) < 0) x += mod;
   return x;
 }
 int qpow(int a, int b = mod - 2) {
   int res = 1;
   while (b > 0) {
     if (b & 1) res = 1ll * res * a % mod;
     a = 1ll * a * a % mod;
     b >>= 1;
   }
   return res;
 }
 namespace Poly {
 vector<int> Rev, W;
 int lim, L;
 void getR(int len) {
   lim = 1, L = 0;
   while (lim <= len) lim <<= 1, L++;
   Rev.resize(lim), W.resize(lim), W[0] = 1;
   for (int i = 0; i < lim; i++)
     Rev[i] = (Rev[i >> 1] >> 1) | ((i & 1) << L - 1);
 }
 void wf(vector<int> &a) {
   int n = a.size();
   for (int i = 0; i < n; i++) a[i] = 1ll * (i + 1) * a[i + 1] % mod;
   a[n - 1] = 0;
 }
 void jf(vector<int> &a) {
   int n = a.size();
   for (int i = n - 1; i >= 1; i--) a[i] = 1ll * a[i - 1] * qpow(i) % mod;
   a[0] = 0;
 }
 void NTT(vector<int> &a, int opt) {
   for (int i = 0; i < lim; i++)
     if (i < Rev[i]) swap(a[i], a[Rev[i]]);
   for (int mid = 1; mid < lim; mid <<= 1) {
     int Wn = qpow(opt == 1 ? G : Gi, (mod - 1) / (mid << 1));
     for (int k = 1; k < mid; k++) W[k] = 1ll * W[k - 1] * Wn % mod;
     for (int j = 0; j < lim; j += mid << 1) {
       for (int k = 0; k < mid; k++) {
         int x = a[j + k], y = 1ll * W[k] * a[j + k + mid] % mod;
         a[j + k] = add(x, y);
         a[j + k + mid] = sub(x, y);
       }
     }
   }
   if (opt == -1) {
     int linv = qpow(lim);
     for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * linv % mod;
   }
 }
 vector<int> operator*(vector<int> a, vector<int> b) {
   int len = a.size() + b.size() - 1;
   getR(len);
   a.resize(lim), b.resize(lim);
   NTT(a, 1), NTT(b, 1);
   for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % mod;
   NTT(a, -1);
   a.resize(len);
   return a;
 }
 vector<int> Inv(vector<int> a) {
   if ((int(a.size())) == 1) return vector<int>(1, qpow(a[0]));
   int len = a.size();
   vector<int> ta = a;
   ta.resize((len + 1) / 2);
   vector<int> tb = Inv(ta);
   getR(2 * len), a.resize(lim), tb.resize(lim);
   NTT(a, 1), NTT(tb, 1);
   for (int i = 0; i < lim; i++)
     tb[i] = 1ll * tb[i] * (mod + 2 - 1ll * a[i] * tb[i] % mod) % mod;
   NTT(tb, -1);
   tb.resize(len);
   return tb;
 }
 vector<int> Ln(vector<int> a) {
   vector<int> ta = a;
   wf(ta);
   int len = a.size();
   a = ta * Inv(a), jf(a);
   a.resize(len);
   return a;
 }
 vector<int> Exp(vector<int> a) {
   if ((int(a.size())) == 1) return vector<int>(1, 1);
   int len = a.size();
   vector<int> ta = a;
   ta.resize((len + 1) / 2);
   vector<int> tb = Exp(ta);
   tb.resize(len);
   vector<int> Lnb = Ln(tb);
   assert(Lnb.size() == len);
   for (int i = 0; i < len; i++) Lnb[i] = (a[i] - Lnb[i] + mod) % mod;
   Lnb[0] = (Lnb[0] + 1) % mod;
   tb = tb * Lnb;
   tb.resize(len);
   return tb;
 }
 }  // namespace Poly
 using namespace Poly;
 const int N = 250005;
 const int B = 15;
 vector<int> adj[N];
 int n;
 int son[N];
 void dfs1(int u, int fa) {
   for (auto v : adj[u]) {
     if (v == fa) continue;
     son[u]++;
     dfs1(v, u);
   }
 }
 int two[N], len;
 vector<int> solve(int l, int r) {
   if (l > r) return {1};
   if (l == r) return {1, mod - two[l]};
   int mid = l + r >> 1;
   return solve(l, mid) * solve(mid + 1, r);
 }
 int fac[N], inv[N];
 int fuck[N];
 int main() {
   n = read();
   inv[1] = 1;
   for (int i = 2; i <= n; i++)
     inv[i] = 1ull * (mod - mod / i) * inv[mod % i] % mod;
   fac[0] = 1;
   for (int i = 1; i <= n; i++) fac[i] = 1ull * fac[i - 1] * i % mod;
   for (int i = 1; i <= n - 1; i++) {
     int u = read(), v = read();
     adj[u].push_back(v), adj[v].push_back(u);
   }
   dfs1(1, 0);
   vector<int> ONE(n + 1);
   for (int i = 1; i <= n; i++) {
     if (son[i] > B) {
       two[++len] = son[i];
     } else {
       fuck[son[i]]++;
     }
   }
   for (int i = 1; i <= B; i++) {
     int coef = 1;
     for (int j = 1; j <= n; j++) {
       coef = 1ull * coef * i % mod;
       ONE[j] = sub(ONE[j], 1ull * fuck[i] * coef % mod * inv[j] % mod);
     }
   }
   ONE = Exp(ONE);
   vector<int> TWO = solve(1, len);
   vector<int> f = ONE * TWO;
   int ans = 0;
   for (int i = 0; i <= n - 1; i++) ans = (ans + 1ll * fac[n - i] * f[i]) % mod;
   print(ans, '
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, arr[200005];
   string swaps;
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> arr[i];
   cin >> swaps;
   int m = 0, flag = 0;
   for (int i = 1; i < n; i++) {
     m = max(m, arr[i]);
     if (swaps[i - 1] == '0' && m > i) {
       flag = 1;
       break;
     }
   }
   (flag) ? cout << 'NO
' : cout << 'YES
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n;
   cin >> n;
   char s;
   int d = 1, j = 1;
   for (int i = 1; i <= n; i++) {
     cin >> s;
     j -= 1;
     if (j == 0) {
       cout << s;
       d += 1;
       j = d;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int m, n;
   cin >> m;
   int a[m];
   for (int i = 0; i < m; i++) {
     a[i] = 0;
   }
   cin >> n;
   int p = 0, q = 0, r = 0;
   for (int i = 0; i < n; i++) {
     cin >> p;
     cin >> q;
     cin >> r;
     a[p - 1] -= r;
     a[q - 1] += r;
   }
   int res = 0;
   for (int i = 0; i < m; i++) {
     if (a[i] > 0) res += a[i];
   }
   cout << res;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 + 10;
 vector<pair<long long, long long> > v[3];
 vector<pair<long long, int> > segment[3];
 struct matrix {
   int arr[3][3];
   matrix(array<int, 3> a1, array<int, 3> a2) {
     for (int i = 0; i <= 2; i++) {
       for (int j = 0; j <= 2; j++) {
         if (!a1[i] and !a2[j] and abs(i - j) < 2) {
           arr[i][j] = 1;
         } else
           arr[i][j] = 0;
       }
     }
   }
   matrix() {
     for (int i = 0; i <= 2; i++) {
       for (int j = 0; j <= 2; j++) {
         if (i == j)
           arr[i][j] = 1;
         else
           arr[i][j] = 0;
       }
     }
   }
 };
 matrix mul(matrix a, matrix b) {
   matrix c;
   for (int i = 0; i <= 2; i++) {
     for (int j = 0; j <= 2; j++) {
       c.arr[i][j] = 0;
       for (int k = 0; k <= 2; k++) {
         c.arr[i][j] = (0ll + c.arr[i][j] +
                        (1ll * a.arr[i][k] * b.arr[k][j]) % 1000000007) %
                       1000000007;
       }
     }
   }
   return c;
 }
 matrix matexpo(matrix a, long long int steps) {
   if (!steps) {
     return matrix();
   }
   matrix temp = matexpo(a, steps / 2);
   temp = mul(temp, temp);
   if (steps % 2) {
     temp = mul(temp, a);
   }
   return temp;
 }
 vector<int> get(vector<int> old_ans, array<int, 3> oldcur, array<int, 3> newcur,
                 long long int steps) {
   matrix alpha(oldcur, newcur);
   matrix temp = matexpo(alpha, steps);
   vector<int> loop_ans(3);
   for (int i = 0; i <= 2; i++) {
     for (int j = 0; j <= 2; j++) {
       loop_ans[j] = (0ll + loop_ans[j] +
                      (1ll * temp.arr[i][j] * old_ans[i]) % 1000000007) %
                     1000000007;
     }
   }
   return loop_ans;
 }
 void solve() {
   long long int n, m;
   scanf('%lld', &n);
   scanf('%lld', &m);
   vector<int> ans = {0, 1, 0};
   array<int, 3> cur = {0, 0, 0};
   for (int i = 1; i <= n; i++) {
     long long int a, l, r;
     scanf('%lld', &a);
     scanf('%lld', &l);
     scanf('%lld', &r);
     v[a - 1].push_back({l, r});
   }
   for (int i = 0; i <= 2; i++) {
     sort((v[i]).begin(), (v[i]).end());
     stack<pair<long long, long long> > s1;
     for (int j = 0; j <= (int)v[i].size() - 1; j++) {
       if (s1.empty() or s1.top().second < (v[i][j].first - 1)) {
         s1.push(v[i][j]);
       } else {
         s1.top().second = max(v[i][j].second, s1.top().second);
       }
     }
     v[i].clear();
     v[i].reserve(s1.size());
     while (!s1.empty()) {
       v[i].push_back(s1.top());
       s1.pop();
     }
     reverse((v[i]).begin(), (v[i]).end());
   }
   for (int i = 0; i <= 2; i++) {
     for (int j = 0; j <= (int)v[i].size() - 1; j++) {
       segment[i].push_back({v[i][j].first, 1});
       segment[i].push_back({v[i][j].second + 1, 0});
     }
     segment[i].push_back({m + 1, 0});
   }
   int i = 0, j = 0, k = 0;
   long long int pos = 1;
   while (true) {
     long long int next =
         min({segment[0][i].first, segment[1][j].first, segment[2][k].first});
     long long int steps = next - pos - 1;
     ans = get(ans, cur, cur, steps);
     if (next == (m + 1)) {
       printf('%d
', ans[1]);
       return;
     }
     pos = next - 1;
     array<int, 3> newcur = cur;
     if (next == segment[0][i].first) {
       newcur[0] = segment[0][i].second;
       i++;
     }
     if (next == segment[1][j].first) {
       newcur[1] = segment[1][j].second;
       j++;
     }
     if (next == segment[2][k].first) {
       newcur[2] = segment[2][k].second;
       k++;
     }
     ans = get(ans, cur, newcur, 1);
     cur = newcur;
     pos++;
   }
 }
 int main() {
   int t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long co[100];
 int main() {
   long long a, l = 0;
   long long ans = 1;
   cin >> a;
   while (a > 1) {
     long long c = 0;
     for (int s = 2; s <= sqrt(a); s++) {
       if (a % s == 0) {
         a /= s, co[l] = s, l++, c = 1;
         break;
       }
     }
     if (c == 0) break;
   }
   if (l == 0)
     cout << 1 << endl << 0;
   else if (l == 1)
     cout << 2;
   else {
     cout << 1 << endl;
     for (int s = 0; s < 2; s++) ans *= co[s];
     cout << ans;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, d, ans = 0;
   scanf('%d%d', &n, &d);
   int maxi, t, l;
   scanf('%d', &maxi);
   for (int i = 1; i < n; i++) {
     scanf('%d', &t);
     if (t > maxi) {
       maxi = t;
       continue;
     } else {
       l = (int)ceil((maxi + 1 - t) / (double)d);
       ans += l;
       maxi = t + d * l;
     }
   }
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e5, M = 1e5;
 int dfn, cur[M + 1], in[N + 1], pre[N + 1], fin[N + 1], r[N + 1], sz[N + 1];
 vector<int> adj[N + 1];
 int dfs(int u, int a) {
   sz[u] = 1;
   r[u] = a;
   pre[u] = ++dfn;
   for (auto v : adj[u]) sz[u] += dfs(v, a);
   fin[u] = ++dfn;
   return sz[u];
 }
 int main() {
   int n, m, k, q;
   scanf('%d%d%d%d', &n, &m, &k, &q);
   for (int i = (0), i_end = (k); i < i_end; ++i) {
     int a, b;
     scanf('%d%d', &a, &b);
     if (cur[b]) {
       ++in[a];
       adj[cur[b]].push_back(a);
     }
     cur[b] = a;
   }
   for (int i = (1), i_end = (n + 1); i < i_end; ++i)
     if (!in[i]) dfs(i, i);
   for (int i = (0), i_end = (q); i < i_end; ++i) {
     int x, y;
     scanf('%d%d', &x, &y);
     y = cur[y];
     if (r[x] == r[y] && pre[y] > pre[x] && fin[y] < fin[x])
       printf('%d
', sz[x]);
     else
       puts('0');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double PI = acos(-1.0);
 const int INF = 1000 * 1000 * 1000 + 7;
 const long long LINF = INF * (long long)INF;
 const int MAX = 2 * 1000 * 100 + 47;
 struct Item {
   int a, t, ind;
   bool operator<(const Item& a) const { return t < a.t; }
 };
 Item A[MAX];
 int n, t;
 bool ok(int k) {
   long long time = 0;
   int cnt = 0;
   for (int i = (0); i < (n); i++) {
     int a = A[i].a;
     int tt = A[i].t;
     if (a >= k && time + tt <= t) {
       cnt++;
       time += tt;
     }
   }
   return cnt >= k;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cin >> n >> t;
   for (int i = (0); i < (n); i++) {
     cin >> A[i].a >> A[i].t;
     A[i].ind = i;
   }
   sort(A, A + n);
   int l = 0, r = n + 1;
   while (r - l > 1) {
     int c = (r + l) / 2;
     if (ok(c)) {
       l = c;
     } else {
       r = c;
     }
   }
   int cnt = l;
   cout << cnt << '
' << cnt << '
';
   for (int i = (0); i < (n); i++) {
     if (cnt == 0) break;
     if (A[i].a >= l) {
       cnt--;
       cout << A[i].ind + 1 << ' ';
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 double a, b, c, d;
 int main() {
   scanf('%d%d%d%d', &a, &b, &c, &d);
   printf('%.12lf
', a / b / (1 - (1 - a / b) * (1 - c / d)));
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e6 + 7;
 int n, p[N];
 deque<pair<int, int> > deq;
 bool mrk[N];
 vector<int> dor[2];
 int dfs(int v, int d) {
   mrk[v] = 1;
   if (!mrk[p[v]]) return dfs(p[v], d + 1);
   return d;
 }
 void add(int v, int l) {
   dor[l].clear();
   int pnt = v;
   do {
     dor[l].push_back(pnt);
     pnt = p[pnt];
   } while (pnt != v);
 }
 void remake(int l) {
   int go = dor[l].size() / 2 + 1;
   for (int i = 0; i < dor[l].size(); i++) {
     p[dor[l][i]] = dor[l][(i + go) % dor[l].size()];
   }
 }
 void merge() {
   for (int i = 0; i < dor[0].size(); i++) {
     p[dor[0][i]] = dor[1][i];
     p[dor[1][i]] = dor[0][(1 + i) % dor[0].size()];
   }
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> p[i];
   for (int i = 1; i <= n; i++) {
     if (!mrk[i]) {
       deq.push_back({dfs(i, 1), i});
     }
   }
   sort(deq.begin(), deq.end());
   while (deq.size()) {
     if (deq[0].first % 2) {
       add(deq[0].second, 0);
       remake(0);
       deq.pop_front();
       continue;
     }
     if (deq.size() == 1 || deq[1].first != deq[0].first) {
       cout << -1;
       return 0;
     }
     add(deq[0].second, 0);
     add(deq[1].second, 1);
     merge();
     deq.pop_front();
     deq.pop_front();
   }
   for (int i = 1; i <= n; i++) cout << p[i] << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 200001;
 int MAX[N];
 vector<int> A[N];
 int main() {
   ios::sync_with_stdio(false);
   set<pair<int, int> > S;
   int n;
   cin >> n;
   for (int i = 0; i < n; i++) {
     int u, v;
     cin >> u >> v;
     MAX[u] = v;
     A[u].push_back(v);
   }
   for (int i = 1; i <= n; i++) {
     if (MAX[i] > 0) S.insert({MAX[i], i});
   }
   int q;
   cin >> q;
   for (int i = 0; i < q; i++) {
     int c;
     cin >> c;
     vector<int> temp;
     for (int j = 0; j < c; j++) {
       int u;
       cin >> u;
       temp.push_back(u);
       S.erase({MAX[u], u});
     }
     if (S.size() > 1) {
       pair<int, int> p = *S.rbegin(), p2;
       S.erase(p);
       temp.push_back(p.second);
       p2 = *S.rbegin();
       int l_lim = 0, u_lim = A[p.second].size();
       while (l_lim < u_lim - 1) {
         int cur = (l_lim + u_lim) / 2;
         if (A[p.second][cur] > p2.first) {
           u_lim = cur;
         } else {
           l_lim = cur;
         }
       }
       if (A[p.second][l_lim] > p2.first) {
         cout << p.second << ' ' << A[p.second][l_lim] << '
';
       } else {
         cout << p.second << ' ' << A[p.second][l_lim + 1] << '
';
       }
     } else if (S.size() == 1) {
       pair<int, int> p = *S.rbegin();
       cout << p.second << ' ' << A[p.second][0] << '
';
     } else {
       cout << '0 0
';
     }
     while (!temp.empty()) {
       if (MAX[temp.back()] > 0) S.insert({MAX[temp.back()], temp.back()});
       temp.pop_back();
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using i64 = long long;
 using u64 = unsigned long long;
 using u32 = unsigned;
 int main() {
   std::ios::sync_with_stdio(false);
   std::cin.tie(nullptr);
   int n;
   std::cin >> n;
   std::vector<int> c0(n, 0);
   std::vector<int> c1(n, 0);
   for (int i = 0; i < n; i++) {
     std::cin >> c0[i] >> c1[i];
   }
   std::vector<int> tmp{0};
   for (int i = 0; i < n; i++) {
     tmp.emplace_back(c0[i]);
     tmp.emplace_back(c1[i]);
   }
   std::sort(tmp.begin(), tmp.end());
   tmp.resize(std::unique(tmp.begin(), tmp.end()) - tmp.begin());
   for (int i = 0; i < n; i++) {
     c0[i] = std::lower_bound(tmp.begin(), tmp.end(), c0[i]) - tmp.begin();
     c1[i] = std::lower_bound(tmp.begin(), tmp.end(), c1[i]) - tmp.begin();
   }
   std::vector<int> p(tmp.size(), 0);
   std::iota(p.begin(), p.end(), 0);
   auto find = [&](int x) {
     while (x != p[x]) {
       x = p[x] = p[p[x]];
     }
     return x;
   };
   int ans = 0;
   for (int i = 0; i < n; i++) {
     int x = c0[i];
     int y = c1[i];
     x = find(x);
     y = find(y);
     if (!x && !y) {
       std::cout << -1, exit(0);
     } else {
       if (x == y || !x || !y) {
         ans = std::max({ans, x, y});
         p[x] = p[y] = 0;
       } else {
         ans = std::max(ans, std::min(x, y));
         p[std::min(x, y)] = std::max(x, y);
       }
     }
   }
   std::cout << tmp[ans] << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct compare {
   bool operator()(const std::string& first, const std::string& second) {
     return first.size() > second.size();
   }
 };
 int main() {
   ios_base::sync_with_stdio(false);
   int n, k, i;
   cin >> n >> k;
   string arr[110];
   for (i = 0; i < n; i++) {
     cin >> arr[i];
   }
   string password;
   cin >> password;
   long long int tries = 0, equallen = 0;
   int plen = password.length();
   for (i = 0; i < n; i++) {
     if (arr[i].length() < plen) {
       tries++;
     } else if (arr[i].length() == plen) {
       equallen++;
     }
   }
   long long int ans = 0;
   ans = ((tries) / k) * 5 + tries + 1;
   cout << ans << ' ';
   ans = ((tries + equallen - 1) / k) * 5 + (tries + equallen);
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string s[10] = {'O-|-OOOO', 'O-|O-OOO', 'O-|OO-OO', 'O-|OOO-O', 'O-|OOOO-',
                   '-O|-OOOO', '-O|O-OOO', '-O|OO-OO', '-O|OOO-O', '-O|OOOO-'};
   string num;
   while (cin >> num) {
     int i = num.size();
     while (i--) cout << s[num[i] - '0'] << endl;
   }
   return 0;
 }


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


#include <bits/stdc++.h>
 using namespace std;
 const long long INF = 1e9 + 7;
 const int N = 2e5 + 10;
 int a[N];
 int main() {
   string str;
   string s;
   while (cin >> s) str += s;
   for (auto& c : str) {
     if (c == '<') c = ' ';
     if (c == '>') c = ' ';
   }
   vector<string> vs;
   vector<int> ans;
   stringstream sin(str);
   while (sin >> str) {
     if (str[0] == '/') {
       if (str == '/table') {
         ans.push_back(a[vs.size() - 1]);
       } else {
         a[vs.size() - 2] += a[vs.size() - 1];
       }
       vs.pop_back();
       a[vs.size()] = 0;
     } else {
       if (str == 'td')
         a[vs.size()] = 1;
       else
         a[vs.size()] = 0;
       vs.push_back(str);
     }
   }
   sort(ans.begin(), ans.end());
   for (auto& x : ans) printf('%d ', x);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int N;
 char field[110][110];
 bool check_solution(bool do_print, bool swapped) {
   for (int r = 0; r < N; ++r) {
     int c;
     for (c = 0; c < N; ++c) {
       if (field[r][c] == '.') break;
     }
     if (c == N) return false;
     if (do_print) {
       if (swapped)
         printf('%d %d
', c + 1, r + 1);
       else
         printf('%d %d
', r + 1, c + 1);
     }
   }
   return true;
 }
 int main(void) {
   scanf('%d', &N);
   for (int r = 0; r < N; ++r) scanf('%s', field[r]);
   if (check_solution(false, false)) {
     check_solution(true, false);
   } else {
     for (int r = 0; r < N; ++r) {
       for (int c = r + 1; c < N; ++c) {
         swap(field[r][c], field[c][r]);
       }
     }
     if (check_solution(false, true))
       check_solution(true, true);
     else
       puts('-1');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct nade {
   int pos, tag, sum, l, r;
 } tree[600005 * 2];
 int n;
 void puup(int x) {
   if (tree[x * 2].sum > tree[x * 2 + 1].sum) {
     tree[x].pos = tree[x * 2].pos;
     tree[x].sum = tree[x * 2].sum;
   } else {
     tree[x].pos = tree[x * 2 + 1].pos;
     tree[x].sum = tree[x * 2 + 1].sum;
   }
 }
 void pudo(int x) {
   tree[x * 2].tag += tree[x].tag;
   tree[x * 2].sum += tree[x].tag;
   tree[x * 2 + 1].tag += tree[x].tag;
   tree[x * 2 + 1].sum += tree[x].tag;
   tree[x].tag = 0;
 }
 void build(int x, int l, int r) {
   tree[x].l = l;
   tree[x].r = r;
   if (l == r) {
     tree[x].pos = l;
     return;
   }
   int mid = (l + r) / 2;
   build(x * 2, l, mid);
   build(x * 2 + 1, mid + 1, r);
   tree[x].pos = tree[x * 2].pos;
 }
 void add(int l, int r, int val, int x) {
   if (tree[x].l >= l && tree[x].r <= r) {
     tree[x].sum += val;
     tree[x].tag += val;
     return;
   }
   pudo(x);
   if (tree[x * 2].r >= l) add(l, r, val, x * 2);
   if (tree[x * 2 + 1].l <= r) add(l, r, val, x * 2 + 1);
   puup(x);
 }
 int l[200005], r[200005], v[200005];
 struct node {
   int x, u, d;
   bool b;
 } s[200005];
 bool cmp(node a, node b) {
   if (a.x != b.x) return a.x < b.x;
   return a.b < b.b;
 }
 int main() {
   scanf('%d', &n);
   for (int i = 0; i < n; i++) {
     scanf('%d%d%d', &l[i], &v[i], &r[i]);
     s[i * 2].x = l[i];
     s[i * 2].u = r[i];
     s[i * 2].d = v[i];
     s[i * 2].b = 0;
     s[i * 2 + 1].x = v[i];
     s[i * 2 + 1].u = r[i];
     s[i * 2 + 1].d = v[i];
     s[i * 2 + 1].b = 1;
   }
   build(1, 1, 3e5 + 1);
   sort(s, s + 2 * n, cmp);
   int ans = 0, ansp = 0, ansx = 0;
   for (int i = 0; i < n * 2; i++) {
     add(s[i].d, s[i].u, s[i].b ? -1 : 1, 1);
     if (tree[1].sum > ans) {
       ans = tree[1].sum;
       ansp = tree[1].pos;
       ansx = s[i].x;
     }
   }
   printf('%d
', ans);
   for (int i = 0; i < n; i++)
     if (l[i] <= ansx && ansx <= v[i] && v[i] <= ansp && ansp <= r[i])
       printf('%d ', i + 1);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 100500;
 const int infi = (1 << 30);
 const long long infl = (1LL << 62);
 const double eps = 1e-9;
 const long long mod = 1000000007LL;
 const double pi = acos(-1.0);
 int n, k;
 int a[maxn], c[maxn];
 int main() {
   cin >> n >> k;
   for (int i = 1; i < n; i++) a[i] = 1;
   a[0] = 0;
   for (int i = 0; i < k; i++) {
     for (int j = 0; j < n; j++) {
       int second = int(upper_bound(a, a + n, j - a[j] - 1) - a);
       if (second == n) second = n - 1;
       c[j] = second;
     }
     for (int j = 0; j < n; j++) {
       a[j] += a[c[j]];
     }
     for (int j = n - 1; j >= 0; --j) cout << n - c[j] << ' ';
     cout << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> divs[1000009];
 bool p[1000009];
 vector<int> primes;
 void pre() {
   for (int i = 2; i <= (1000000); ++i) {
     if (!p[i]) {
       primes.push_back(i);
       for (int j = i; j <= 1000000; j += i) divs[j].push_back(i), p[j] = true;
     }
   }
 }
 int L(int x, int p) {
   int s = (int)(divs[p]).size();
   int tot = (1 << s) - 1;
   long long ans = 0;
   for (int i = 1; i <= (tot); ++i) {
     int cnt = 0;
     int var = 1;
     for (int j = 0; j < (s); ++j)
       if (i & (1 << j)) cnt++, var *= divs[p][j];
     if (cnt % 2)
       ans += x / var;
     else
       ans -= x / var;
   }
   return x - ans;
 }
 int main() {
   cin.sync_with_stdio(0);
   cin.tie(0);
   cin.exceptions(cin.failbit);
   pre();
   int q;
   cin >> q;
   for (int aa = 0; aa < (q); ++aa) {
     int x, p, k;
     cin >> x >> p >> k;
     if (p == 1)
       cout << k + x << '
';
     else {
       k += L(x, p);
       int l = 1, r = 1e8;
       while (l < r) {
         int m = (l + r) >> 1;
         if (L(m, p) >= k)
           r = m;
         else
           l = m + 1;
       }
       cout << l << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 class Solution {
  public:
   long long count(long long _m, long long _x) {
     if (_m < 3) {
       return max(0LL, _m);
     }
     m = _m;
     x = _x;
     vector<pair<long long, int>> cnts = factorize(m);
     long long res = 0;
     search(0, 1, cnts, res);
     return res;
   }

  private:
   map<long long, vector<pair<long long, int>>> fs;
   long long m, x;
   long long gcd(long long x, long long y) {
     while (x != 0) {
       y %= x;
       swap(x, y);
     }
     return y;
   }
   vector<pair<long long, int>>& factorize(long long n) {
     if (fs.count(n) > 0) {
       return fs[n];
     }
     vector<pair<long long, int>>& res = fs[n];
     for (long long i = 2; i * i <= n; ++i) {
       int cnt = 0;
       while (n % i == 0) {
         ++cnt;
         n /= i;
       }
       if (cnt > 0) {
         res.emplace_back(i, cnt);
       }
     }
     if (n > 1) {
       res.emplace_back(n, 1);
     }
     return res;
   }
   void search(int i, long long dv, vector<pair<long long, int>>& cnts,
               long long& ans) {
     if (i >= cnts.size()) {
       vector<pair<long long, int>> dvcnts;
       dvcnts.reserve(cnts.size());
       for (auto& pc : cnts) {
         if (pc.second <= 1) {
           continue;
         }
         dvcnts.emplace_back(pc.first, pc.second - 1);
       }
       long long phi = dv;
       for (auto& pc : cnts) {
         long long p = pc.first;
         int c = pc.second;
         if (c == 0) {
           continue;
         }
         dvcnts = merge(dvcnts, factorize(p - 1));
         phi = phi - phi / p;
       }
       ans += phi / calcPeriod(x % dv, dv, dvcnts);
       return;
     }
     long long p = fs[m][i].first;
     long long n = fs[m][i].second;
     for (int j = 0; j <= n; ++j) {
       cnts[i].second = j;
       search(i + 1, dv, cnts, ans);
       dv *= p;
     }
   }
   vector<pair<long long, int>> merge(vector<pair<long long, int>>& list1,
                                      vector<pair<long long, int>>& list2) {
     vector<pair<long long, int>> res;
     res.reserve(list1.size() + list2.size());
     int n = list1.size(), m = list2.size();
     int i = 0, j = 0;
     while (i < n && j < m) {
       if (list1[i].first < list2[j].first) {
         res.emplace_back(list1[i].first, list1[i].second);
         ++i;
       } else if (list1[i].first > list2[j].first) {
         res.emplace_back(list2[j].first, list2[j].second);
         ++j;
       } else {
         res.emplace_back(list1[i].first, list1[i].second + list2[j].second);
         ++i;
         ++j;
       }
     }
     while (i < n) {
       res.emplace_back(list1[i].first, list1[i].second);
       ++i;
     }
     while (j < m) {
       res.emplace_back(list2[j].first, list2[j].second);
       ++j;
     }
     return res;
   }
   long long calcPeriod(long long x, long long mod,
                        vector<pair<long long, int>>& factors) {
     long long phi = 1;
     for (auto& pc : factors) {
       long long p = pc.first;
       int c = pc.second;
       for (int i = 0; i < c; ++i) {
         phi *= p;
       }
     }
     long long res = 1;
     for (auto& pc : factors) {
       long long p = pc.first;
       int c = pc.second;
       long long base = phi;
       for (int i = 0; i < c; ++i) {
         base /= p;
       }
       long long val = powM(x, base, mod);
       int j = c;
       for (int i = 1; i <= c; ++i) {
         if (val == 1) {
           j = i - 1;
           break;
         }
         val = powM(val, p, mod);
       }
       res *= powI(p, j);
     }
     return res;
   }
   long long mulM(long long a, long long b, long long m) {
     long long res = 0;
     while (b > 0) {
       if (b & 0x1) {
         res = (res + a) % m;
       }
       a = (a + a) % m;
       b >>= 1;
     }
     return res;
   }
   long long powM(long long x, long long e, long long m) {
     long long res = 1;
     while (e > 0) {
       if (e & 0x1) {
         res = mulM(res, x, m);
       }
       x = mulM(x, x, m);
       e >>= 1;
     }
     return res;
   }
   long long powI(long long x, long long e) {
     long long res = 1;
     for (int i = 0; i < e; ++i) {
       res *= x;
     }
     return res;
   }
 };
 int main(int argc, char** argv) {
   ios::sync_with_stdio(false);
   cin.tie(0);
   long long m, x;
   cin >> m >> x;
   Solution sol;
   cout << sol.count(m, x) << endl;
   return 0;
 }