#include <bits/stdc++.h>
 using namespace std;
 template <class Key, class Value>
 struct Heap {
   Value p[100100];
   int pos[100100];
   vector<Key> hp;
   void init() { hp.clear(); }
   void swapPos(int i, int a) {
     int temp;
     temp = pos[hp[a]];
     pos[hp[a]] = pos[hp[i]];
     pos[hp[i]] = temp;
     swap(hp[i], hp[a]);
   }
   void up(int a) {
     int i;
     while (1) {
       i = a;
       if (a + a + 1 < hp.size() && p[hp[i]] > p[hp[a + a + 1]]) {
         i = a + a + 1;
       }
       if (a + a + 2 < hp.size() && p[hp[i]] > p[hp[a + a + 2]]) {
         i = a + a + 2;
       }
       if (i == a) break;
       swapPos(i, a);
       a = i;
     }
   }
   void down(int a) {
     int i, temp;
     while (1) {
       i = a;
       if (a && p[hp[i]] < p[hp[(a - 1) / 2]]) {
         i = (a - 1) / 2;
       }
       if (i == a) break;
       swapPos(i, a);
       a = i;
     }
   }
   void update(Key k, Value v) {
     p[k] = v;
     up(pos[k]);
     down(pos[k]);
   }
   void push_heap(Key k, Value v) {
     hp.push_back(k);
     p[k] = v;
     down(pos[k] = hp.size() - 1);
   }
   Value top() { return p[hp[0]]; }
   void pop(Key& k, Value& v) {
     k = hp[0];
     v = p[hp[0]];
     swapPos(0, hp.size() - 1);
     hp.pop_back();
     up(0);
   }
   int size() { return hp.size(); }
 };
 Heap<int, long long int> hp;
 template <class T>
 struct Graph {
   static const int gmaxn = 101000;
   static const int gmaxm = 101000;
   int ind[gmaxn];
   int last[gmaxm * 2], e[gmaxm * 2], gt;
   T w[gmaxm * 2];
   bool bi;
   int n, m;
   void init() {
     gt = 0;
     memset(ind, 0, sizeof(ind));
   }
   void addE1(int a, int b, T& c) {
     gt++;
     last[gt] = ind[a];
     ind[a] = gt;
     e[gt] = b;
     w[gt] = c;
   }
   void addE2(int a, int b, T& c) {
     addE1(a, b, c);
     addE1(b, a, c);
   }
   void addE(int a, int b, T& c) {
     if (bi)
       addE2(a, b, c);
     else
       addE1(a, b, c);
   }
   Graph(bool bidirectional = true) { bi = bidirectional; }
   void next(int& a) { a = last[a]; }
   void dijkstra(vector<int>& S);
 };
 int from[100100];
 long long int dis[100100];
 template <class T>
 void Graph<T>::dijkstra(vector<int>& S) {
   memset(dis, -1, sizeof(dis));
   int i, now, j;
   long long int nowdis;
   for (i = 0; i < S.size(); i++) {
     dis[from[S[i]] = S[i]] = 0;
     hp.push_heap(S[i], 0);
   }
   while (hp.size()) {
     hp.pop(now, nowdis);
     for (i = ind[now]; i; next(i)) {
       j = e[i];
       if (dis[j] < 0) {
         dis[j] = nowdis + w[i];
         from[j] = from[now];
         hp.push_heap(j, dis[j]);
       } else if (dis[j] > nowdis + w[i]) {
         dis[j] = nowdis + w[i];
         from[j] = from[now];
         hp.update(j, dis[j]);
       }
     }
   }
 }
 Graph<long long int> g;
 bool k[100100];
 vector<int> s;
 int fa[100100];
 int findfa(int a) {
   if (a == fa[a]) return a;
   return fa[a] = findfa(fa[a]);
 }
 bool merge(int a, int b) {
   a = findfa(a);
   b = findfa(b);
   if (a == b) return 0;
   if (rand() & 1) swap(a, b);
   fa[a] = b;
   return 1;
 }
 vector<pair<long long int, pair<int, int> > > eg;
 int main() {
   scanf('%d %d', &g.n, &g.m);
   int i, j, a, b, cc;
   long long int c;
   for (i = 0; i < g.m; i++) {
     scanf('%d %d %d', &a, &b, &cc);
     c = cc;
     g.addE(a, b, c);
   }
   scanf('%d', &i);
   while (i--) {
     scanf('%d', &j);
     k[j] = 1;
     s.push_back(j);
   }
   g.dijkstra(s);
   for (i = 1; i <= g.n; i++) {
     fa[i] = i;
     for (j = g.ind[i]; j; g.next(j)) {
       if (from[i] != from[g.e[j]]) {
         eg.push_back(make_pair(dis[i] + dis[g.e[j]] + g.w[j],
                                make_pair(from[i], from[g.e[j]])));
       }
     }
   }
   sort(eg.begin(), eg.end());
   long long int ans = 0;
   if (!k[1]) ans += dis[1];
   for (i = 0; i < eg.size(); i++) {
     if (merge(eg[i].second.first, eg[i].second.second)) {
       ans += eg[i].first;
     }
   }
   printf('%I64d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = INT_MAX;
 const int mod = 1e9 + 7;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     int arr[2][n];
     int sum[2][n];
     for (int i = 0; i < 2; i++) {
       for (int j = 0; j < n; j++) {
         cin >> arr[i][j];
         if (j == 0)
           sum[i][j] = arr[i][j];
         else
           sum[i][j] = sum[i][j - 1] + arr[i][j];
       }
     }
     int move1 = sum[0][n - 1];
     int move2 = sum[1][n - 1];
     int ans = min(move1 - arr[0][0], move2 - arr[1][n - 1]);
     for (int i = 1; i < n; i++) {
       int temp1 = move1 - sum[0][i];
       int temp2 = sum[1][i - 1];
       ans = min(ans, max(temp1, temp2));
     }
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 #pragma comment(linker, '/STACK:167772160')
 using namespace std;
 int main() {
   int n;
   cin >> n;
   pair<int, int> x[1111];
   for (int i = 0; i < n; ++i) cin >> x[i].first >> x[i].second;
   long long p = 0, maxx = -1;
   int j = 0;
   for (int i = 0; i < x[n - 1].first; i++) {
     if (p != 0) --p;
     if (i == x[j].first - 1) {
       p += x[j].second;
       ++j;
     }
     if (p > maxx) maxx = p;
   }
   cout << x[n - 1].first + p << ' ' << maxx << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int n, k, a, b;
 vector<long long int> arr;
 long long int rec(long long int l, long long int r) {
   long long int i = lower_bound(arr.begin(), arr.end(), l) - arr.begin();
   long long int j = upper_bound(arr.begin(), arr.end(), r) - arr.begin();
   long long int x = j - i;
   long long int val;
   if (x == 0) {
     val = a;
   } else {
     val = b * x * (r - l + 1);
   }
   if (l == r || x == 0) {
     return val;
   }
   long long int mid = (l + r) / 2;
   return min(val, rec(l, mid) + rec(mid + 1, r));
 }
 int main() {
   cin >> n >> k >> a >> b;
   for (int i = 0; i < k; i++) {
     int x;
     cin >> x;
     arr.push_back(x);
   }
   sort(arr.begin(), arr.end());
   long long int start = 1;
   long long int end = pow(2, n);
   cout << rec(start, end) << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s[10];
 int main() {
   scanf('%s', s);
   int res = 1;
   for (int i = 1; i < 7; i++) {
     if (s[i] == '1')
       res += 10, i++;
     else
       res += s[i] - '0';
   }
   printf('%d
', res);
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e6 + 10;
 char s[N];
 int main() {
   vector<int> v1, v2;
   scanf('%s', s);
   int len = strlen(s);
   for (int i = 0; i < len; i++) {
     if (s[i] == 'l') {
       v1.push_back(i + 1);
     } else
       v2.push_back(i + 1);
   }
   for (int x : v2) printf('%d
', x);
   for (int i = (int)v1.size() - 1; i >= 0; i--) printf('%d
', v1[i]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e5 + 100;
 int n, p[4], bx[N], wx, by[N], wy, sum[N][4];
 vector<int> wh[N];
 struct node {
   int x, y, c;
 } sh[N];
 bool cmp(node a, node b) { return a.x < b.x; }
 inline int read() {
   int f = 1, x = 0;
   char s = getchar();
   while (s < '0' || s > '9') {
     if (s == '-') f = -1;
     s = getchar();
   }
   while (s >= '0' && s <= '9') {
     x = x * 10 + s - '0';
     s = getchar();
   }
   return x * f;
 }
 struct bit {
   int sh[N];
   void init() { memset(sh, 0, sizeof(sh)); }
   int lowbit(int x) { return x & (-x); }
   void change(int x, int v) {
     while (x <= n) sh[x] += v, x += lowbit(x);
   }
   int query(int x) {
     int ans = 0;
     while (x) ans += sh[x], x -= lowbit(x);
     return ans;
   }
   int ask(int l, int r) { return query(r) - query(l - 1); }
 } T[4];
 int g(int l, int r, int c) { return sum[r][c] - sum[l - 1][c]; }
 void init() {
   memset(sum, 0, sizeof(sum));
   wx = wy = 0;
   for (int i = 1; i <= n; i++) bx[++wx] = sh[i].x;
   sort(bx + 1, bx + 1 + wx);
   wx = unique(bx + 1, bx + 1 + wx) - bx - 1;
   for (int i = 1; i <= n; i++)
     sh[i].x = lower_bound(bx + 1, bx + 1 + wx, sh[i].x) - bx;
   for (int i = 1; i <= n; i++) by[++wy] = sh[i].y;
   sort(by + 1, by + 1 + wy);
   wy = unique(by + 1, by + 1 + wy) - by - 1;
   for (int i = 1; i <= n; i++)
     sh[i].y = lower_bound(by + 1, by + 1 + wy, sh[i].y) - by;
   sort(sh + 1, sh + 1 + n, cmp);
   for (int i = 1; i <= n; i++) sum[sh[i].x][sh[i].c]++;
   for (int i = 1; i <= wx; i++)
     for (int j = 1; j <= 3; j++) sum[i][j] += sum[i - 1][j];
 }
 int solve() {
   for (int i = 1; i <= wx; i++) wh[i].clear();
   for (int i = 1; i <= n; i++) wh[sh[i].x].push_back(i);
   for (int i = 1; i <= 3; i++) p[i] = i;
   int ans = 1;
   do {
     for (int i = 2; i <= wx; i++) {
       int A = sum[i - 1][p[1]], l = i, r = wx - 1;
       while (l < r) {
         int mid = l + ((r - l + 1) >> 1);
         if (g(i, mid, p[2]) <= g(mid + 1, wx, p[3]))
           l = mid;
         else
           r = mid - 1;
       }
       ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])}));
       l++;
       ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])}));
     }
   } while (next_permutation(p + 1, p + 4));
   for (int i = 1; i <= 3; i++) p[i] = i;
   auto find = [](int A, int &ans) {
     int l = 1, r = wy - 1;
     while (l < r) {
       int mid = l + ((r - l + 1) >> 1);
       if (T[p[2]].ask(1, mid) <= T[p[3]].ask(mid + 1, wy))
         l = mid;
       else
         r = mid - 1;
     }
     ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)}));
     l++;
     ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)}));
   };
   do {
     T[p[2]].init();
     T[p[3]].init();
     for (int i = wx; i >= 1; i--) {
       int A = g(1, i, p[1]);
       find(A, ans);
       for (int j : wh[i])
         if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1);
     }
   } while (next_permutation(p + 1, p + 4));
   for (int i = 1; i <= 3; i++) p[i] = i;
   do {
     T[p[2]].init();
     T[p[3]].init();
     for (int i = 1; i <= wx; i++) {
       int A = g(i, wx, p[1]);
       find(A, ans);
       for (int j : wh[i])
         if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1);
     }
   } while (next_permutation(p + 1, p + 4));
   return ans;
 }
 signed main() {
   n = read();
   for (int i = 1; i <= n; i++) sh[i] = (node){read(), read(), read()};
   init();
   int ans = solve();
   for (int i = 1; i <= n; i++) swap(sh[i].x, sh[i].y);
   init();
   ans = max(ans, solve());
   printf('%d
', ans * 3);
 }


#include <bits/stdc++.h>
 static const int MAXN = 50004;
 static const int MAXE = 100004 * 2;
 static const int MAXL = 20;
 static const int MAXT = 1000005;
 int n, e;
 int head[MAXN], dest[MAXE], next[MAXE];
 int l[MAXN], t[MAXN][MAXL];
 int T = 0;
 int p[MAXN];
 int conf[MAXN], team[MAXN];
 int team_conf[MAXT];
 inline void add_edge(int u, int v) {
   static int w = 0;
   dest[w] = v;
   next[w] = head[u];
   head[u] = w++;
 }
 int main() {
   scanf('%d%d', &n, &e);
   for (int i = 0; i < n; ++i) head[i] = -1;
   int u, v;
   for (int i = 0; i < e; ++i) {
     scanf('%d%d', &u, &v);
     --u, --v;
     add_edge(u, v);
     add_edge(v, u);
   }
   for (int i = 0; i < n; ++i) {
     scanf('%d', &l[i]);
     for (int j = 0; j < l[i]; ++j) {
       scanf('%d', &t[i][j]);
       --t[i][j];
       T = std::max(T, t[i][j]);
     }
   }
   ++T;
   srand(87665544);
   for (int i = 0; i < n; ++i) p[i] = i;
   for (int attempts = 0;; ++attempts) {
     if (attempts % 16 == 0) {
       std::random_shuffle(p, p + n);
       std::fill(conf, conf + n, -1);
       int ct[2];
       for (int i = 0; i < n; ++i) {
         ct[0] = ct[1] = 0;
         for (int w = head[p[i]]; w != -1; w = next[w]) {
           if (conf[dest[w]] != -1) ++ct[conf[dest[w]]];
         }
         conf[p[i]] = (ct[0] > ct[1] ? 1 : 0);
       }
     }
     for (int i = 0; i < T; ++i) team_conf[i] = rand() % 2;
     bool valid = true;
     for (int i = 0; i < n; ++i) {
       team[i] = -1;
       for (int j = 0; j < l[i]; ++j)
         if (conf[i] == team_conf[t[i][j]]) {
           team[i] = t[i][j];
           break;
         }
       if (team[i] == -1) {
         valid = false;
         break;
       }
     }
     if (!valid) continue;
     for (int i = 0; i < n; ++i)
       printf('%d%c', team[i] + 1, i == n - 1 ? '
' : ' ');
     for (int i = 0; i < T; ++i)
       printf('%d%c', team_conf[i] + 1, i == T - 1 ? '
' : ' ');
     break;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m;
 int ans = 654;
 vector<int> a, b, us(10);
 int main() {
   cin >> n >> m;
   a.resize(n);
   b.resize(m);
   for (int i = 0; i < n; ++i) {
     cin >> a[i];
     us[a[i]] = 1;
   }
   for (int j = 0; j < m; ++j) {
     cin >> b[j];
     if (us[b[j]] == 1) {
       ans = min(ans, b[j]);
     }
   }
   sort(a.begin(), a.end());
   sort(b.begin(), b.end());
   cout << min({(a[0] * 10) + b[0], (b[0] * 10) + a[0], ans});
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MOD = 1000000007;
 int dp[1001][2010];
 int digit[1002], k;
 void init() { memset(dp, -1, sizeof dp); }
 int dfs(int pos, int dist, bool doing) {
   if (pos == -1) return dist == 0;
   if (!doing && dp[pos][dist] != -1) return dp[pos][dist];
   int ans = 0;
   int end = doing ? digit[pos] : 9;
   for (int i = 0; i <= end; i++) {
     int nextdist = dist == 0 ? 0 : dist + 1;
     if (i == 4 || i == 7) {
       if (dist <= k)
         nextdist = 0;
       else
         nextdist = 1;
     }
     ans = (ans + dfs(pos - 1, nextdist, doing && i == end)) % MOD;
   }
   if (!doing) dp[pos][dist] = ans;
   return ans;
 }
 int cal(char x[]) {
   int pos = 0, l = strlen(x);
   while (pos < l) {
     digit[pos] = x[l - pos - 1] - '0';
     pos++;
   }
   return dfs(pos - 1, k + 2, 1);
 }
 bool check(char x[]) {
   int dist = k + 1;
   for (int i = 0; i < strlen(x); i++) {
     if (!dist) break;
     if (x[i] == '4' || x[i] == '7') {
       if (dist <= k)
         dist = 0;
       else
         dist = 1;
     } else
       dist++;
   }
   return dist == 0;
 }
 int main() {
   int T;
   init();
   char a[1002], b[1002];
   scanf('%d %d', &T, &k);
   while (T--) {
     scanf('%s %s', a, b);
     int ansa = cal(a);
     int ansb = cal(b);
     if (check(a)) ansa--;
     ansb = ((ansb - ansa) % MOD + MOD) % MOD;
     printf('%d
', ansb);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<string> vec_splitter(string s) {
   s += ',';
   vector<string> res;
   while (!s.empty()) {
     res.push_back(s.substr(0, s.find(',')));
     s = s.substr(s.find(',') + 1);
   }
   return res;
 }
 void debug_out(vector<string> __attribute__((unused)) args,
                __attribute__((unused)) int idx,
                __attribute__((unused)) int LINE_NUM) {
   cerr << '
';
 }
 template <typename Head, typename... Tail>
 void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
   if (idx > 0)
     cerr << ', ';
   else
     cerr << 'Line(' << LINE_NUM << ') ';
   stringstream ss;
   ss << H;
   cerr << args[idx] << ' = ' << ss.str();
   debug_out(args, idx + 1, LINE_NUM, T...);
 }
 void run() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr), cout.tie(nullptr);
 }
 const int N = 5e4 + 4, MOD = 1e9 + 7, mod2 = 998244353;
 int dp[N][503];
 int n, k;
 vector<int> adj[N];
 long long ans;
 void dfs(int u, int p) {
   dp[u][0] = 1;
   for (auto &v : adj[u]) {
     if (v == p) continue;
     dfs(v, u);
     for (int i = 0; i < k; ++i) {
       dp[u][i + 1] += dp[v][i];
     }
   }
   long long sum = 0;
   for (auto &v : adj[u]) {
     if (v == p) continue;
     for (int i = 1; i < k; ++i) {
       sum += dp[v][i - 1] * (dp[u][k - i] - dp[v][k - i - 1]);
     }
   }
   ans += dp[u][k] + sum / 2;
 }
 int main() {
   run();
   cin >> n >> k;
   for (int i = 0; i < (int)(n - 1); ++i) {
     int u, v;
     cin >> u >> v;
     adj[v].push_back(u);
     adj[u].push_back(v);
   }
   dfs(1, -1);
   cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 void Genawy() {
   std::ios_base::sync_with_stdio(0);
   cin.tie(NULL);
 }
 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
 int n, cost[305];
 int length[305];
 map<pair<int, int>, int> dp;
 int solve(int ind, int gc) {
   if (ind == n) return gc == 1 ? 0 : 1e9;
   if (dp.find({ind, gc}) != dp.end()) return dp[{ind, gc}];
   dp[{ind, gc}] = solve(ind + 1, gc);
   dp[{ind, gc}] =
       min(dp[{ind, gc}], solve(ind + 1, gcd(gc, length[ind])) + cost[ind]);
   return dp[{ind, gc}];
 }
 int main() {
   Genawy();
   cin >> n;
   for (int i = 0; i < n; i++) cin >> length[i];
   for (int i = 0; i < n; i++) cin >> cost[i];
   int ans = solve(0, 0);
   if (ans >= 1e9) ans = -1;
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 int compare(const void *a, const void *b) {
   long long la = *(long long *)a;
   long long lb = *(long long *)b;
   return la == lb ? 0 : la < lb ? -1 : 1;
 }
 long long aa[8], aa_[8];
 int bb[8], dp[8][1 << (8 - 1)];
 char pp[8];
 int n, ans = 0x3f3f3f3f;
 void solve(int i) {
   int j;
   if (i == n) {
     int cnt, sum;
     cnt = 0, sum = 0;
     for (i = 0; i < n; i++) {
       int k;
       if (!pp[i]) cnt++;
       k = dp[i][bb[i]];
       sum += k == 1 && bb[i] == 0 ? 1 : k + 1;
     }
     if (cnt >= 2) sum++;
     if (ans > sum) ans = sum;
     return;
   }
   solve(i + 1);
   pp[i] = 1;
   for (j = i + 1; j < n; j++)
     if (aa_[j] % aa[i] == 0) {
       aa_[j] /= aa[i], bb[j] ^= 1 << i;
       solve(i + 1);
       aa_[j] *= aa[i], bb[j] ^= 1 << i;
     }
   pp[i] = 0;
 }
 int main() {
   int i, j;
   scanf('%d', &n);
   for (i = 0; i < n; i++) scanf('%lld', &aa[i]);
   qsort(aa, n, sizeof *aa, compare);
   for (i = 0; i < n; i++) {
     long long a;
     int k, p, b;
     a = aa[i];
     k = 0;
     for (p = 2; p <= a / p; p++)
       while (a % p == 0) {
         k++;
         a /= p;
       }
     if (a > 1) k++;
     dp[i][0] = k;
     for (b = 1; b < 1 << i; b++) {
       a = aa[i];
       k = dp[i][0];
       for (j = 0; j < i; j++)
         if ((b & 1 << j)) {
           if (a % aa[j] != 0) {
             k = -1;
             break;
           }
           a /= aa[j];
           k -= dp[j][0];
         }
       dp[i][b] = k;
     }
   }
   for (i = 0; i < n; i++) aa_[i] = aa[i];
   solve(0);
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<string> v;
 int main() {
   string x, y;
   int cnt = 0;
   char temp;
   int n;
   cin >> n;
   n = n + n - 1;
   for (int i = 0; i < n; i++) {
     cin >> x;
     if (x == '**') continue;
     cin >> y;
     x += y;
     sort(v.begin(), v.end());
     if (binary_search(v.begin(), v.end(), x)) continue;
     cnt++;
     v.push_back(x);
     for (int j = 1; j <= 3; j++) {
       y = '';
       if (j == 1) {
         y += x[2];
         y += x[0];
         y += x[3];
         y += x[1];
         v.push_back(y);
       } else if (j == 2) {
         y += x[3];
         y += x[2];
         y += x[1];
         y += x[0];
         v.push_back(y);
       } else if (j == 3) {
         y += x[1];
         y += x[3];
         y += x[0];
         y += x[2];
         v.push_back(y);
       }
     }
   }
   cout << cnt;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e5 + 5;
 const int inf = 1 << 30;
 int n, qn, tot = 0, head[N], col[N], nowHead[N], deg[N], seq[N];
 int dfsc = 0, id[N], siz[N], fa[N], son[N], dep[N], top[N];
 bool vis[N];
 struct Edge {
   int to, nxt;
 } e[N << 1];
 inline void add(int from, int to) {
   e[++tot].to = to;
   e[tot].nxt = head[from];
   head[from] = tot;
 }
 struct Seg {
   int l, r, c;
   inline Seg(int L = 0, int R = 0, int C = 0) { l = L, r = R, c = C; }
   friend bool operator<(const Seg x, const Seg y) { return x.l < y.l; }
 };
 set<Seg> seg[N];
 template <typename T>
 inline void read(T &X) {
   X = 0;
   char ch = 0;
   T op = 1;
   for (; ch > '9' || ch < '0'; ch = getchar())
     if (ch == '-') op = -1;
   for (; ch >= '0' && ch <= '9'; ch = getchar())
     X = (X << 3) + (X << 1) + ch - 48;
   X *= op;
 }
 namespace Bit {
 int s[N << 1];
 inline void modify(int p, int v) {
   if (!p) return;
   for (; p < (N << 1); p += (p & (-p))) s[p] += v;
 }
 inline int query(int p) {
   int res = 0;
   for (; p > 0; p -= (p & (-p))) res += s[p];
   return res;
 }
 }  // namespace Bit
 void dfs1(int x, int fat, int depth) {
   fa[x] = fat, dep[x] = depth, siz[x] = 1;
   int maxson = -1;
   for (int i = head[x]; i; i = e[i].nxt) {
     int y = e[i].to;
     if (y == fat) continue;
     dfs1(y, x, depth + 1);
     siz[x] += siz[y];
     if (siz[y] > maxson) {
       maxson = siz[y];
       son[x] = y;
     }
   }
 }
 void dfs2(int x, int topf) {
   top[x] = topf, id[x] = ++dfsc;
   if (!son[x]) return;
   dfs2(son[x], topf);
   for (int i = head[x]; i; i = e[i].nxt) {
     int y = e[i].to;
     if (y == fa[x] || y == son[x]) continue;
     dfs2(y, y);
   }
 }
 inline int getLca(int x, int y) {
   for (; top[x] != top[y];) {
     if (dep[top[x]] < dep[top[y]]) swap(x, y);
     x = fa[top[x]];
   }
   return dep[x] < dep[y] ? x : y;
 }
 inline int getDis(int x, int y) {
   return dep[x] + dep[y] - 2 * dep[getLca(x, y)];
 }
 inline set<Seg>::iterator getSeg(set<Seg> &s, int l) {
   set<Seg>::iterator it = s.lower_bound(Seg(l, inf, 0));
   if (it != s.end() && it->l == l) return it;
   --it;
   int nowl = it->l, nowr = it->r, nowc = it->c;
   s.erase(it);
   if (l - 1 >= nowl) s.insert(Seg(nowl, l - 1, nowc));
   s.insert(Seg(l, nowr, nowc));
   return s.lower_bound(Seg(l, nowr, nowc));
 }
 inline void ins(set<Seg> &s, int l, int r, int c, int st) {
   set<Seg>::iterator itl = getSeg(s, l), itr;
   if (r + 1 <= id[st] + siz[st] - 1)
     itr = getSeg(s, r + 1);
   else
     itr = s.end();
   for (set<Seg>::iterator it = itl; it != itr; ++it) {
     Bit ::modify(it->c, -((it->r) - (it->l) + 1));
   }
   Bit ::modify(c, r - l + 1);
   s.erase(itl, itr), s.insert(Seg(l, r, c));
 }
 inline void modify(int x, int y, int c) {
   nowHead[c] = y;
   for (; top[x] != top[y];) {
     if (dep[top[x]] < dep[top[y]]) swap(x, y);
     ins(seg[top[x]], id[top[x]], id[x], c, top[x]);
     x = fa[top[x]];
   }
   if (dep[x] > dep[y]) swap(x, y);
   ins(seg[top[x]], id[x], id[y], c, top[x]);
 }
 inline int solve(int x) {
   int c = getSeg(seg[top[x]], id[x])->c;
   return Bit ::query(c - 1) + getDis(x, nowHead[c]) + 1;
 }
 int main() {
   read(n), read(qn);
   for (int x, y, i = 1; i < n; i++) {
     read(x), read(y);
     ++deg[x], ++deg[y];
     add(x, y), add(y, x);
   }
   dfs1(1, 0, 1), dfs2(1, 1);
   int cnt = 0;
   set<int> q;
   for (int i = 1; i <= n; i++)
     if (deg[i] == 1) q.insert(i), vis[i] = 1;
   for (; !q.empty();) {
     int now = (*q.begin());
     q.erase(*q.begin());
     seq[now] = ++cnt;
     nowHead[cnt] = now;
     seg[top[now]].insert(Seg(id[now], id[now], cnt));
     Bit ::modify(cnt, 1);
     for (int i = head[now]; i; i = e[i].nxt) {
       if (vis[e[i].to]) continue;
       --deg[e[i].to];
       if (deg[e[i].to] == 1 && !vis[e[i].to])
         q.insert(e[i].to), vis[e[i].to] = 1;
     }
   }
   int lst = n;
   for (char op[9]; qn--;) {
     scanf('%s', op);
     if (op[0] == 'u') {
       int x;
       read(x);
       modify(x, lst, ++cnt);
       lst = x;
     }
     if (op[0] == 'w') {
       int x;
       read(x);
       printf('%d
', solve(x));
     }
     if (op[0] == 'c') {
       int x, y;
       read(x), read(y);
       printf('%d
', solve(x) < solve(y) ? x : y);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = (int)1e9 + 7;
 int up(int n) {
   int r = 1;
   while (r < n) r <<= 1;
   return r;
 }
 class segtree {
   vector<int> arr;
   vector<bool> laz;
   void inv(int l, int r, int cl, int cr, int v) {
     if (r <= cl || cr <= l) return;
     push(v);
     if (l <= cl && cr <= r) {
       laz[v] = !laz[v];
       return;
     }
     int ct = (cl + cr) / 2;
     inv(l, r, cl, ct, 2 * v);
     inv(l, r, ct, cr, 2 * v + 1);
     upd(v);
   }
   int cnt(int l, int r, int cl, int cr, int v) {
     if (r <= cl || cr <= l) return 0;
     push(v);
     if (l <= cl && cr <= r) return arr[v];
     int ct = (cl + cr) / 2;
     ct = cnt(l, r, cl, ct, 2 * v) + cnt(l, r, ct, cr, 2 * v + 1);
     upd(v);
     return ct;
   }
   void push(int v) {
     if (laz[v]) {
       laz[v] = false;
       arr[v] = arr.size() / up(v + 1) - arr[v];
       if (v < (int)arr.size() / 2) {
         laz[2 * v] = !laz[2 * v];
         laz[2 * v + 1] = !laz[2 * v + 1];
       }
     }
   }
   void upd(int v) {
     if (v < (int)arr.size() / 2) {
       push(2 * v);
       push(2 * v + 1);
       arr[v] = arr[2 * v] + arr[2 * v + 1];
     }
   }

  public:
   segtree(int n) : arr(2 * up(n)), laz(2 * up(n)) {}
   void inv(int l, int r) { inv(l, r, 0, arr.size() / 2, 1); }
   int cnt(int l, int r) { return cnt(l, r, 0, arr.size() / 2, 1); }
 };
 void dfs(const vector<vector<int>> &gr, int v, vector<int> &in,
          vector<int> &out, int &cnt) {
   in[v] = cnt++;
   for (auto it : gr[v]) dfs(gr, it, in, out, cnt);
   out[v] = ++cnt;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n;
   cin >> n;
   vector<vector<int>> gr(n);
   for (int i = 1; i < n; i++) {
     int p;
     cin >> p;
     p--;
     gr[p].push_back(i);
   }
   vector<int> in(n), out(n);
   n = 0;
   dfs(gr, 0, in, out, n);
   segtree st(n);
   n /= 2;
   for (int i = 0; i < n; i++) {
     int t;
     cin >> t;
     if (t == 1) {
       st.inv(in[i], in[i] + 1);
       st.inv(out[i] - 1, out[i]);
     }
   }
   int q;
   cin >> q;
   for (int i = 0; i < q; i++) {
     string s;
     int t;
     cin >> s >> t;
     t--;
     if (s == 'get') {
       cout << st.cnt(in[t], out[t]) / 2 << endl;
     } else {
       assert(s == 'pow');
       st.inv(in[t], out[t]);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     long long int a[n], sum = 0, fg = 0, cnt = 0, ind = 1, ans = 0;
     for (int i = 0; i < n; i++) cin >> a[i];
     for (int i = 0; i < n; i++) {
       if (a[i] <= ind) {
         ind++;
         continue;
       }
       ans = ans + a[i] - ind;
       ind = a[i] + 1;
     }
     cout << ans;
     cout << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   string s;
   cin >> s;
   s = '*' + s;
   int add = 0;
   int n = (s.length()) - 1;
   for (int i = 1; i + i <= n; i++) {
     if (s[i] != s[s.size() - i]) {
       add++;
     }
   }
   if (add <= 1 && (n & 1) || add == 1)
     cout << 'YES
';
   else
     cout << 'NO
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, a, i, j, arr[300] = {0}, p = 0, k = 0, c, d;
   cin >> n >> a;
   for (i = 1; i < n + 1; i++) {
     cin >> arr[i];
   }
   arr[0] = 1, arr[n + 1] = 1;
   j = min(a, n - a + 1);
   for (i = j; i < n + 1; i++) {
     c = a + k;
     d = a - k;
     if (d < 1) d = 0;
     if (c > n) c = n + 1;
     if (arr[d] == arr[c] and arr[c]) {
       if (c != d and c <= n and d > 0)
         p += 2;
       else
         p++;
     }
     k++;
   }
   cout << p << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct node {
   int x, y;
 } arr[104];
 vector<int> graph[105];
 int visited[105] = {0};
 void dfs(int i) {
   visited[i] = 1;
   int j = 0;
   while (j < graph[i].size()) {
     if (visited[graph[i][j]] == 0) {
       dfs(graph[i][j]);
     }
     j++;
   }
 }
 int main() {
   int n;
   scanf('%d', &n);
   int x, y;
   for (int i = 0; i < n; i++) {
     scanf('%d%d', &x, &y);
     arr[i].x = x;
     arr[i].y = y;
   }
   int hash[102] = {0};
   int ct = 0;
   for (int i = 0; i < n; i++) {
     int flag = 0;
     for (int j = 0; j < n; j++) {
       if (i == j) continue;
       if (arr[j].x == arr[i].x || arr[j].y == arr[i].y) {
         graph[i].push_back(j);
         graph[j].push_back(i);
         flag = 1;
       }
     }
     if (flag == 0) visited[i] = 1;
   }
   for (int i = 0; i < n; i++) {
     if (visited[i] == 0) {
       dfs(i);
       ct++;
     }
   }
   int c = 0;
   for (int i = 0; i < n; i++) {
     if (!graph[i].size()) c++;
   }
   printf('%d
', c - 1 + ct);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXT = (1 << 18) + 5;
 int icT[MAXT], dcT[MAXT];
 const int MAXN = 100010;
 int a[MAXN], idxofa[MAXN], b[MAXN];
 int n;
 priority_queue<pair<int, int> > dc, ic;
 int main() {
   scanf('%d', &n);
   for (int i = 1; i <= (int)(n); i++) {
     scanf('%d', &a[i]);
     idxofa[a[i]] = i;
   }
   for (int i = 0; i < (int)(MAXT); i++) icT[i] = dcT[i] = 1000000000;
   int NN = (1 << 17) - 1;
   for (int i = 1; i <= (int)(n); i++) {
     scanf('%d', &b[i]);
     if (i > idxofa[b[i]]) {
       dc.push(make_pair(-(i - idxofa[b[i]]), b[i]));
       dcT[NN + b[i]] = abs(i - idxofa[b[i]]);
       for (int node = (NN + b[i]) / 2; node; node >>= 1)
         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]);
     } else {
       ic.push(make_pair(-(i - 1), b[i]));
       icT[NN + b[i]] = abs(i - idxofa[b[i]]);
       for (int node = (NN + b[i]) / 2; node; node >>= 1)
         icT[node] = min(icT[2 * node], icT[2 * node + 1]);
     }
   }
   for (int t = 0; t < n; t++) {
     int dcres = dcT[1] - t;
     int icres = icT[1] + t;
     printf('%d
', min(dcres, icres));
     while (dc.size() > 0 && -dc.top().first == t) {
       pair<int, int> qt = dc.top();
       dc.pop();
       ic.push(make_pair(-t - (idxofa[qt.second] - 1), qt.second));
       dcT[NN + qt.second] = 1000000000;
       icT[NN + qt.second] = -t;
       for (int node = (NN + qt.second) / 2; node; node >>= 1)
         icT[node] = min(icT[2 * node], icT[2 * node + 1]);
       for (int node = (NN + qt.second) / 2; node; node >>= 1)
         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]);
     }
     while (ic.size() > 0 && -ic.top().first == t) {
       pair<int, int> qt = ic.top();
       ic.pop();
       dc.push(make_pair(-t - (n - idxofa[qt.second]) - 1, qt.second));
       icT[NN + qt.second] = 1000000000;
       dcT[NN + qt.second] = t + 1 + n - idxofa[qt.second];
       for (int node = (NN + qt.second) / 2; node; node >>= 1)
         icT[node] = min(icT[2 * node], icT[2 * node + 1]);
       for (int node = (NN + qt.second) / 2; node; node >>= 1)
         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 2e5 + 10;
 const long long inf = 0x3f3f3f3f3f3f3f3f;
 int n, k, b, c;
 int a[N], A[N], cost[N];
 long long ans = inf;
 void solve(int x) {
   for (int i = 1; i <= n; ++i) {
     A[i] = a[i];
     cost[i] = 0;
     while (A[i] % 5 != x) ++A[i], cost[i] += c;
   }
   priority_queue<long long> heap;
   long long res = 0;
   for (int i = 1; i <= k; ++i) {
     long long val = 5 * cost[i] - 1LL * b * A[i];
     res += val;
     heap.push(val);
   }
   ans = min(ans, (res + 1LL * k * b * A[k]) / 5);
   for (int i = k + 1; i <= n; ++i) {
     long long val = 5 * cost[i] - 1LL * b * A[i];
     if (val < heap.top()) {
       res -= heap.top();
       heap.pop();
       heap.push(val);
       res += val;
     }
     ans = min(ans, (res + 1LL * k * b * A[i]) / 5);
   }
 }
 int main() {
   cin >> n >> k >> b >> c;
   for (int i = 1; i <= n; ++i) {
     scanf('%d', &a[i]);
     a[i] += 1e9;
   }
   sort(a + 1, a + n + 1);
   b = min(b, c * 5);
   for (int i = 0; i < 5; ++i) solve(i);
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 const int N = 1e5 + 5;
 const int OO = 1e9 + 7;
 using namespace std;
 long long n, m, dishes[N], cost[N];
 priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                greater<pair<long long, long long>>>
     q;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ;
   cin >> n >> m;
   for (int i = 0; i < n; i++) cin >> dishes[i];
   for (int i = 0; i < n; i++) cin >> cost[i], q.push({cost[i], i});
   for (int i = 0; i < m; i++) {
     long long kind, numofdishes, COST = 0;
     cin >> kind >> numofdishes;
     kind--;
     if (numofdishes <= dishes[kind]) {
       COST += (numofdishes * cost[kind]);
       dishes[kind] -= numofdishes;
       cout << COST << '
';
     } else {
       COST += (dishes[kind] * cost[kind]);
       numofdishes -= dishes[kind];
       dishes[kind] = 0;
       while (!q.empty() && numofdishes > 0) {
         if (dishes[q.top().second] <= 0) {
           q.pop();
           continue;
         }
         if (numofdishes <= dishes[q.top().second]) {
           COST += (numofdishes * q.top().first);
           dishes[q.top().second] -= numofdishes;
           numofdishes = 0;
         } else {
           COST += (dishes[q.top().second] * q.top().first);
           numofdishes -= dishes[q.top().second];
           dishes[q.top().second] = 0;
         }
         if (dishes[q.top().second] <= 0) {
           q.pop();
           continue;
         }
       }
       if (numofdishes > 0)
         cout << 0 << '
';
       else
         cout << COST << '
';
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void myswap(int& a, int& b) {
   int temp;
   temp = a;
   a = b;
   b = temp;
 }
 void fun(vector<int>& vec, int k, int l, int r) {
   if (k <= 1 || l >= r - 1) {
     return;
   }
   int mid = (l + r) / 2;
   if (k == 3) {
     myswap(vec[mid - 1], vec[mid]);
     return;
   }
   k--;
   int p = k / 2;
   if (p % 2 == 1) {
     fun(vec, p, l, mid);
     fun(vec, p, mid, r);
   } else if (mid - l >= r - mid) {
     fun(vec, p + 1, l, mid);
     fun(vec, p - 1, mid, r);
   } else {
     fun(vec, p - 1, l, mid);
     fun(vec, p + 1, mid, r);
   }
 }
 int _count(int l, int r) {
   if (l >= r) {
     return 0;
   }
   if (l == r - 1) {
     return 1;
   }
   int mid = (l + r) / 2;
   return 1 + _count(l, mid) + _count(mid, r);
 }
 int main() {
   int n, k, i, j;
   cin >> n >> k;
   if (k % 2 == 0) {
     cout << -1;
   } else if (k == 1) {
     for (i = 1; i <= n; i++) {
       cout << i << ' ';
     }
   } else {
     int mm = _count(0, n);
     if (k > mm) {
       cout << -1;
     } else {
       vector<int> vec;
       for (i = 1; i <= n; i++) {
         vec.push_back(i);
       }
       fun(vec, k, 0, n);
       for (i = 0; i < n; i++) {
         cout << vec[i] << ' ';
       }
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     int n, k;
     cin >> n >> k;
     string s;
     cin >> s;
     int freq[26] = {0};
     vector<int> v;
     for (int i = 0; i < s.length(); ++i) {
       freq[s[i] - 'a']++;
     }
     for (int i = 0; i < 26; ++i) {
       v.push_back(freq[i]);
     }
     sort(v.begin(), v.end());
     int maxAns = 1;
     for (int i = 1; i <= k; ++i) {
       if (k % i == 0) {
         for (int j = i; j <= n; j += i) {
           int ans = 0;
           int div = j / i;
           for (int k = 0; k < v.size(); ++k) {
             ans += v[k] / div;
           }
           if (ans >= i) {
             maxAns = max(maxAns, j);
           }
         }
       }
     }
     cout << maxAns << endl;
   }
 }


#include <bits/stdc++.h>
 int t;
 long long n, k, d1, d2, td1, td2, x1, x2, x3;
 bool solve() {
   if (n % 3) return false;
   for (int i = -1; i <= 1; ++i)
     for (int j = -1; j <= 1; ++j) {
       if (i == 0 || j == 0) continue;
       d1 = i * td1;
       d2 = j * td2;
       if ((d1 * 2 + d2 + k) % 3 || (d2 - d1 + k) % 3 || (k - d1 - 2 * d2) % 3)
         continue;
       x1 = d1 * 2 + d2 + k;
       x2 = d2 - d1 + k;
       x3 = k - d1 - 2 * d2;
       if (x1 <= n && x2 <= n && x3 <= n && x1 >= 0 && x2 >= 0 && x3 >= 0)
         return true;
     }
   return false;
 }
 int main() {
   scanf('%d', &t);
   while (t--) {
     scanf('%lld%lld%lld%lld', &n, &k, &td1, &td2);
     puts(solve() ? 'yes' : 'no');
   }
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int n;
   while (scanf('%d', &n) != EOF) {
     if (n % 2 == 1) {
       printf('-1
');
       continue;
     }
     printf('2 1');
     for (int i = 3; i < n; i = i + 2) printf(' %d %d', i + 1, i);
     printf('
');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 map<int, int> Map;
 pair<int, int> a[250001];
 vector<int> b[250001], t[250001];
 char type;
 long long ans;
 int len[250001], n, m, i, q, l, r, d, mid, nom, nomN, zn, sum;
 bool pole[250001];
 int get(int tl, int tr, int nom, int l, int r, int nomT) {
   if (tl > tr) return 0;
   if (tl > r || tr < l) return 0;
   if (l <= tl && tr <= r) return t[nomT][nom];
   int mid = (tl + tr) / 2;
   return get(tl, mid, nom * 2, l, r, nomT) +
          get(mid + 1, tr, nom * 2 + 1, l, r, nomT);
 }
 void update(int tl, int tr, int nom, int nomN, int zn, int nomT) {
   if (tl > tr) return;
   if (tl > nomN || tr < nomN) return;
   t[nomT][nom] += zn;
   if (tl == tr) return;
   int mid = (tl + tr) / 2;
   update(tl, mid, nom * 2, nomN, zn, nomT);
   update(mid + 1, tr, nom * 2 + 1, nomN, zn, nomT);
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cin >> n >> m >> q;
   for (i = 0; i < n; i++)
     if (!pole[i]) {
       nom = i;
       d++;
       t[d].push_back(0);
       b[d].push_back(0);
       while (!pole[nom]) {
         pole[nom] = true;
         len[d]++;
         a[nom + 1] = make_pair(d, len[d]);
         b[d].push_back(nom);
         t[d].push_back(0);
         t[d].push_back(0);
         t[d].push_back(0);
         t[d].push_back(0);
         nom = (nom + m) % n;
       }
     }
   while (q--) {
     cin >> type;
     if (type == '+') {
       cin >> zn >> nom;
       nom++;
       sum = 0;
       if (get(1, len[a[nom].first], 1, a[nom].second, len[a[nom].first],
               a[nom].first) != len[a[nom].first] - a[nom].second + 1) {
         l = a[nom].second;
         r = len[a[nom].first];
         while (l <= r) {
           mid = (l + r) / 2;
           if (get(1, len[a[nom].first], 1, a[nom].second, mid, a[nom].first) !=
               mid - a[nom].second + 1) {
             r = mid - 1;
             sum = mid - a[nom].second;
           } else
             l = mid + 1;
         }
         update(1, len[a[nom].first], 1, a[nom].second + sum, 1, a[nom].first);
         Map[zn] = b[a[nom].first][a[nom].second + sum] + 1;
       } else {
         ans += len[a[nom].first] - a[nom].second + 1;
         l = 1;
         r = a[nom].second - 1;
         while (l <= r) {
           mid = (l + r) / 2;
           if (get(1, len[a[nom].first], 1, 1, mid, a[nom].first) != mid) {
             r = mid - 1;
             sum = mid - 1;
           } else
             l = mid + 1;
         }
         update(1, len[a[nom].first], 1, sum + 1, 1, a[nom].first);
         Map[zn] = b[a[nom].first][sum + 1] + 1;
       }
       ans += sum;
     } else {
       cin >> zn;
       nom = Map[zn];
       update(1, len[a[nom].first], 1, a[nom].second, -1, a[nom].first);
     }
   }
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long int nax = 1e5 + 5;
 void solve() {
   long long int n;
   cin >> n;
   vector<long long int> a(n);
   for (auto &x : a) cin >> x;
   long long int sum = 0, mx = 0;
   for (long long int i = 0; i < n - 1; i++) {
     sum += max((long long int)0, a[i] - a[i + 1]);
   }
   cout << sum << '
';
 }
 int32_t main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   long long int t = 1;
   cin >> t;
   while (t--) solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline int read() {
   int n = 0, f = 1, ch = getchar();
   while (ch < '0' || ch > '9') {
     if (ch == '-') f = -1;
     ch = getchar();
   }
   while (ch >= '0' && ch <= '9') {
     n = n * 10 + ch - '0';
     ch = getchar();
   }
   return n * f;
 }
 char a[2000005], b[2000005];
 int n;
 int c[2000005];
 int get(int x) {
   int cur = 0, res = 0;
   for (int i = 1; i <= n; i++) {
     cur += x * c[i];
     res = max(res, cur);
     if (cur < 0) cur = 0;
   }
   return res;
 }
 int main() {
   int s1 = 0, s2 = 0;
   n = read();
   scanf('%s%s', a + 1, b + 1);
   for (int i = 1; i <= n; i++) {
     if (a[i] != b[i] && a[i] == '0')
       c[i] = -1, s1++;
     else if (a[i] != b[i] && a[i] == '1')
       c[i] = 1, s2++;
   }
   if (s1 != s2) {
     printf('-1
');
     return 0;
   }
   printf('%d
', max(get(1), get(-1)));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long double sq(long double a) { return a * a; }
 long double dist(pair<long double, long double> a,
                  pair<long double, long double> b) {
   return sqrt((long double)(sq(a.second - b.second) + sq(a.first - b.first)));
 }
 long double mandist(pair<long long, long long> a,
                     pair<long long, long long> b) {
   return abs(a.second - b.second) + abs(a.first - b.first);
 }
 long long a, b, c;
 pair<long long, long long> p1, p2;
 int main() {
   scanf(' %lld%lld%lld', &a, &b, &c);
   scanf(' %lld%lld%lld%lld', &p1.first, &p1.second, &p2.first, &p2.second);
   if (a == 0 || b == 0)
     printf('%.20Lf
', mandist(p1, p2));
   else {
     long double res = mandist(p1, p2);
     long double interhoriz1 =
         ((long double)(-c - a * p1.first)) / ((long double)b);
     long double interhoriz2 =
         ((long double)(-c - a * p2.first)) / ((long double)b);
     long double intervert1 =
         ((long double)(-c - b * p1.second)) / ((long double)a);
     long double intervert2 =
         ((long double)(-c - b * p2.second)) / ((long double)a);
     res = min(res, abs((long double)p1.second - interhoriz1) +
                        abs((long double)p2.second - interhoriz2) +
                        dist(make_pair((long double)p1.first, interhoriz1),
                             make_pair((long double)p2.first, interhoriz2)));
     res = min(res, abs((long double)p1.second - interhoriz1) +
                        abs((long double)p2.first - intervert2) +
                        dist(make_pair((long double)p1.first, interhoriz1),
                             make_pair(intervert2, (long double)p2.second)));
     res = min(res, abs((long double)p1.first - intervert1) +
                        abs((long double)p2.first - intervert2) +
                        dist(make_pair(intervert1, (long double)p1.second),
                             make_pair(intervert2, (long double)p2.second)));
     res = min(res, abs((long double)p1.first - intervert1) +
                        abs((long double)p2.second - interhoriz2) +
                        dist(make_pair(intervert1, (long double)p1.second),
                             make_pair((long double)p2.first, interhoriz2)));
     printf('%.20Lf
', res);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void solve();
 signed main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout << fixed;
   cout.precision(12);
   solve();
   return 0;
 }
 template <typename T>
 void prv(vector<T> v) {
   for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
     if (__ii) cout << ' ';
     cout << v[__ii];
   }
   cout << '
';
 }
 template <typename T>
 void prm(vector<vector<T>> v) {
   for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
     for (int __jj = 0; __jj < v[__ii].size(); __jj++) {
       if (__jj) cout << ' ';
       cout << v[__ii][__jj];
     }
     cout << '
';
   }
 }
 template <typename T>
 void sc(T& x) {
   cin >> x;
 }
 template <typename Head, typename... Tail>
 void sc(Head& head, Tail&... tail) {
   cin >> head;
   sc(tail...);
 }
 template <typename T>
 void pr(const T& x) {
   cout << x << '
';
 }
 template <typename Head, typename... Tail>
 void pr(const Head& head, const Tail&... tail) {
   cout << head << ' ';
   pr(tail...);
 }
 template <typename... T>
 void err(const T&... cod) {
   pr(cod...);
   exit(0);
 }
 void solve() {
   int n, m, y, x;
   sc(n, m, y, x);
   string s;
   sc(s);
   int dx = (s[1] == 'L' ? -1 : 1);
   int dy = (s[0] == 'U' ? -1 : 1);
   const int MAX_ITER = 1e6 + 10;
   set<pair<int, int>> st;
   long long ans = 1;
   for (int i = 0; i < MAX_ITER; i++) {
     st.emplace(y, x);
     if (((int)st.size()) == n + m - 2) err(ans);
     int u = (dy == -1 ? y - 1 : n - y);
     int v = (dx == -1 ? x - 1 : m - x);
     int w = min(u, v);
     x += dx * w;
     y += dy * w;
     ans += w;
     if (v == w) dx *= -1;
     if (u == w) dy *= -1;
   }
   pr(-1);
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   cin.tie(0);
   cout.tie(0);
   ios_base::sync_with_stdio(0);
   string s;
   cin >> s;
   string v = 'aeiou';
   string c = 'bcdfghjklmnpqrstvwxyz';
   map<char, int> cnt;
   map<char, int> mp;
   for (auto i : v) mp[i] = 1;
   for (auto i : c) mp[i] = 0;
   string temp = '';
   int vc = 0;
   int cc = 0;
   int dc = 0;
   vector<string> ans;
   for (int i = 0; i < s.size(); i++) {
     if (mp[s[i]] == 1) {
       cc = 0;
       dc = 0;
       for (auto j : c) cnt[j] = 0;
     } else {
       if (cnt[s[i]] == 0) {
         cnt[s[i]]++;
         dc++;
         cc++;
       } else {
         cnt[s[i]]++;
         cc++;
       }
     }
     if (cc > 2 && dc > 1) {
       ans.push_back(temp);
       for (auto j : c) cnt[j] = 0;
       cnt[s[i]] = 1;
       cc = 1;
       dc = 1;
       temp = s[i];
     } else
       temp += s[i];
   }
   ans.push_back(temp);
   for (auto i : ans) cout << i << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e6 + 5;
 string s[10005];
 int vis[10005];
 int main() {
   ios::sync_with_stdio(0);
   int n, sum(0);
   cin >> n;
   for (long long i = 0; i < n; ++i) cin >> s[i], sum += s[i].size();
   char c;
   cin >> c;
   for (long long i = 0; i < n; ++i) s[i] += c;
   sort(s, s + n);
   sum = sum * 2 / n + 1;
   for (long long i = 0; i < n; ++i)
     if (!vis[i]) {
       cout << s[i];
       vis[i]++;
       for (long long j = i + 1; j < n; ++j)
         if (!vis[j] && sum - s[i].size() == s[j].size() - 1) {
           cout << s[j].substr(0, s[j].size() - 1) << '
';
           vis[j]++;
           break;
         }
     }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   vector<int> a(n);
   for (int i = 0; i < n; ++i) cin >> a[i];
   map<int, int> used;
   int ans = 0;
   for (int i = n - 1; i >= 0; --i) {
     if (used[a[i]] == 0) {
       used[a[i]] = i;
       ans = a[i];
     }
   }
   cout << ans;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long double E = 1e-7;
 const int MOD = 1e9 + 7;
 const long long MAX = 1e6 + 1;
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ;
   int n, k;
   cin >> n >> k;
   int ar[n];
   for (int i = 0; i < n; i++) cin >> ar[i];
   int ans = INT_MIN;
   int a[n], b[n];
   for (int i = 0; i < n; i++)
     for (int j = i; j < n; j++) {
       int A = j - i + 1;
       int B = n - A;
       copy(ar + i, ar + j + 1, a);
       copy(ar, ar + i, b);
       copy(ar + j + 1, ar + n, b + i);
       sort(a, a + A);
       sort(b, b + B);
       for (int l = 0, r = B - 1, q = k; l < A && r >= 0 && a[l] < b[r] && q;
            l++, r--, --q)
         swap(a[l], b[r]);
       int sum = 0;
       for (int i = 0; i < A; i++) sum += a[i];
       ans = max(ans, sum);
     }
   cout << ans;
 }


#include <bits/stdc++.h>
 using namespace std;
 int gcd(int x, int y) {
   if (x > y)
     return gcd(x - y, y);
   else if (x == y)
     return x;
   else
     return gcd(x, y - x);
 }
 int main() {
   int n, m, z;
   cin >> n >> m >> z;
   cout << (gcd(n, m) * z) / (n * m) << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, cnt, flag = 0;
   cin >> n;
   int a, b, c = 0;
   for (int i = 1; i <= n; i++) {
     cin >> a >> b;
     if (b - a >= 2) ++c;
   }
   cout << c << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, p = 0, ans = 0;
   string s, dvg, answ, r_dvg;
   cin >> n >> s;
   for (int i = 0; i < n - 1; i++) {
     dvg = s.substr(i, 2);
     for (int j = i; j < n - 1; j++) {
       r_dvg = s.substr(j, 2);
       if (dvg == r_dvg) {
         p++;
       }
     }
     if (p > ans) {
       ans = p;
       answ = dvg;
     }
     p = 0;
   }
   cout << answ << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Point {
   long long x, y;
   void read() { scanf('%lld%lld', &x, &y); }
 };
 long long product(const Point &o, const Point &a, const Point &b) {
   return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
 }
 bool intersect(const Point &a, const Point &b, const Point &c, const Point &d) {
   bool insect1 = product(a, c, d) * product(b, c, d) < 0;
   bool insect2 = product(c, a, b) * product(d, a, b) < 0;
   return insect1 && insect2;
 }
 int main() {
   ios ::sync_with_stdio(false);
   cout << fixed << setprecision(16);
   int R, B;
   scanf('%d%d', &R, &B);
   vector<Point> bases(B);
   vector<Point> ships(R);
   vector<int> b(R);
   for (int i = 0; i < R; i++) {
     ships[i].read();
   }
   for (int i = 0; i < B; i++) {
     bases[i].read();
   }
   if (B != R) {
     puts('No');
     return 0;
   }
   for (int i = 0; i < R; i++) {
     b[i] = i;
   }
   bool yes = false;
   do {
     bool ok = true;
     for (int i = 0; i < B; i++) {
       for (int j = i + 1; j < B; j++) {
         if (intersect(ships[b[i]], bases[i], ships[b[j]], bases[j])) {
           ok = false;
         }
       }
     }
     if (ok) {
       yes = true;
       break;
     }
   } while (next_permutation(b.begin(), b.end()));
   puts(yes ? 'Yes' : 'No');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long maxn = 305000;
 long long mod = 1e9 + 7;
 long long h, w, n;
 long long dp[maxn], f[maxn], inv[maxn];
 struct stu {
   long long x, y;
 } a[maxn];
 long long cmp(stu a, stu b) {
   if (a.x != b.x)
     return a.x < b.x;
   else
     return a.y < b.y;
 }
 long long ksm(long long base, long long b, long long mod) {
   long long ans = 1;
   while (b) {
     if (b & 1) {
       ans *= base;
       ans %= mod;
     }
     base *= base;
     base %= mod;
     b >>= 1;
   }
   return ans;
 }
 void init() {
   f[0] = 1, inv[0] = 1;
   for (long long i = 1; i <= 200000; i++) {
     f[i] = f[i - 1] * i % mod;
     inv[i] = ksm(f[i], mod - 2, mod);
   }
 }
 long long c(long long x, long long y) {
   if (x < y)
     return 0;
   else
     return f[x] * inv[y] % mod * inv[x - y] % mod;
 }
 signed main() {
   cin >> h >> w >> n;
   init();
   for (long long i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
   sort(a + 1, a + n + 1, cmp);
   a[n + 1].x = h, a[n + 1].y = w;
   for (long long i = 1; i <= n + 1; i++) {
     dp[i] = c(a[i].x + a[i].y - 2, a[i].x - 1);
     for (long long j = 1; j < i; j++) {
       if (a[j].x > a[i].x || a[j].y > a[i].y) continue;
       dp[i] = (dp[i] -
                dp[j] * c(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x)) %
               mod;
     }
   }
   cout << (dp[n + 1] + mod) % mod << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int v[3005], inv = 0, n;
 double sol;
 int main() {
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> v[i];
   for (int i = 1; i <= n; i++)
     for (int j = i + 1; j <= n; j++)
       if (v[i] > v[j]) inv++;
   sol = double(inv) - inv / 2 + (inv / 2) * 3.0;
   cout << fixed;
   cout << setprecision(6);
   cout << sol << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = 1000 * 1000 * 1000;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int n;
   cin >> n;
   pair<int, int> minp1(INF, INF), minp2(INF, INF);
   for (int i = 0; i < n; i++) {
     int x, y;
     cin >> x >> y;
     minp1 = min(minp1, pair<int, int>(x, y));
   }
   for (int i = 0; i < n; i++) {
     int x, y;
     cin >> x >> y;
     x *= -1;
     y *= -1;
     minp2 = min(minp2, pair<int, int>(x, y));
   }
   cout << minp1.first - minp2.first << ' ' << minp1.second - minp2.second;
 }


#include <bits/stdc++.h>
 const int MAXN = 100100;
 struct Gao {
   int p[MAXN], q[MAXN];
   bool r[MAXN];
   void init(int n) {
     for (int i = 0; i < n; ++i) {
       q[i] = 1;
       r[i] = true;
     }
   }
   void next(int i) {
     if (!r[p[i]] && p[i] != i) {
       next(p[i]);
       q[i] += q[p[i]];
       p[i] = p[p[i]];
     }
   }
   void kill(int i) {
     if (r[i]) {
       next(i);
       r[i] = false;
     }
   }
 } gao;
 int main() {
   int n, m, x, y, z;
   static int a[MAXN], b[MAXN];
   scanf('%d%d', &n, &m);
   gao.init(n);
   for (int i = 0; i < n; ++i) {
     scanf('%d', &gao.p[i]);
     --gao.p[i];
   }
   for (int i = 0; i < m; ++i) {
     scanf('%d', &a[i]);
     --a[i];
   }
   for (int i = 0; i < m; ++i) {
     scanf('%d', &b[i]);
   }
   z = 0;
   for (int i = 0; i < m; ++i) {
     x = (a[i] + z) % n;
     y = b[i];
     z = 0;
     while (true) {
       if (!gao.r[x]) {
         gao.next(x);
         y -= gao.q[x];
         x = gao.p[x];
       }
       if (gao.r[x] && y > 0) {
         ++z;
         gao.kill(x);
       } else {
         break;
       }
     }
     printf('%d
', z);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 4e4 + 5;
 int pr[N], ps = 0;
 bool np[N];
 bool check(int n) {
   if (n == 1 || n == 0) return false;
   for (int i = 0; pr[i] * pr[i] <= n; ++i)
     if (n % pr[i] == 0) return false;
   return true;
 }
 int main() {
   for (int i = 2; i < N; ++i)
     if (!np[i]) {
       pr[ps++] = i;
       for (int j = i + i; j < N; j += i) np[j] = 1;
     }
   int n;
   cin >> n;
   if (check(n)) {
     cout << '1
' << n << endl;
     return 0;
   }
   for (int i = 0; i < ps && pr[i] <= n; ++i) {
     if (check(n - pr[i])) {
       cout << '2
' << pr[i] << ' ' << n - pr[i] << endl;
       return 0;
     }
     if (pr[i] % 2 == 1 && check((n - pr[i]) / 2)) {
       cout << '3
'
            << pr[i] << ' ' << (n - pr[i]) / 2 << ' ' << (n - pr[i]) / 2 << endl;
       return 0;
     }
   }
   cout << 'lol nope
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long mk = 100005;
 long long n;
 vector<long long> g[mk];
 long long find(long long A) {
   if (A == 0) return 0;
   long long tmp = A & 3;
   long long R = (find(A / 4) << 2);
   if (tmp == 0) return R;
   if (tmp == 1) return (R | 2);
   if (tmp == 2) return (R | 3);
   if (tmp == 3) return (R | 1);
 }
 void init() {
   long long test;
   cin >> test;
   for (long long i = 1; i <= test; i++) {
     long long n;
     cin >> n;
     long long root = 1;
     while (root * 4 <= n) root *= 4;
     n -= (root - 1);
     long long A = (root | ((n - 1) / 3));
     long long B = find(A);
     long long C = A ^ B;
     if (n % 3 == 1) cout << A;
     if (n % 3 == 2) cout << B;
     if (n % 3 == 0) cout << C;
     cout << '
';
   }
 }
 signed main() { init(); }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e4 + 5;
 pair<int, int> p[maxn];
 set<pair<int, int> > s;
 void solve(int l, int r) {
   if (r - l < 2) return;
   int mid = (l + r) / 2;
   solve(l, mid), solve(mid, r);
   for (int i = l; i < r; i++) s.insert(make_pair(p[mid].first, p[i].second));
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   int n;
   cin >> n;
   for (int i = 0; i < n; i++)
     cin >> p[i].first >> p[i].second,
         s.insert(make_pair(p[i].first, p[i].second));
   sort(p, p + n);
   solve(0, n);
   cout << s.size() << endl;
   for (pair<int, int> now : s) cout << now.first << ' ' << now.second << endl;
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int n;
   long long int s;
   scanf('%d', &n);
   s = 6 * n * (n - 1) + 1;
   printf('%I64d', s);
   return 0;
 }


#include <bits/stdc++.h>
 template <class C, class E>
 inline bool contains(const C& container, const E& element) {
   return container.find(element) != container.end();
 }
 template <class T>
 inline void checkmin(T& a, T b) {
   if (b < a) a = b;
 }
 template <class T>
 inline void checkmax(T& a, T b) {
   if (b > a) a = b;
 }
 using namespace std;
 class UnionFind {
  public:
   UnionFind(int elements) : _subsets(elements), _setCount(elements) {
     for (int i = 0; i < elements; ++i) {
       _subsets[i].parent = i;
       _subsets[i].rank = 0;
     }
   }
   int Find(int i) {
     if (_subsets[i].parent != i) _subsets[i].parent = Find(_subsets[i].parent);
     return _subsets[i].parent;
   }
   void Union(int x, int y) {
     int xroot = Find(x);
     int yroot = Find(y);
     if (xroot == yroot) return;
     --_setCount;
     if (_subsets[xroot].rank < _subsets[yroot].rank)
       _subsets[xroot].parent = yroot;
     else if (_subsets[xroot].rank > _subsets[yroot].rank)
       _subsets[yroot].parent = xroot;
     else {
       _subsets[yroot].parent = xroot;
       _subsets[xroot].rank++;
     }
   }
   struct subset {
     int parent;
     int rank;
   };
   void Trace() {
     for (int i = (0), _b = ((_subsets.size()) - 1); i <= _b; i++)
       cout << i << ':' << Find(i) << ' |';
     cout << endl;
   }
   vector<subset> _subsets;
   int _setCount;
 };
 struct SetInfo {
   set<int> _yc;
   set<int> _xc;
 };
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   cout << std::setprecision(15);
   cout << std::fixed;
   long long xs, ys, q;
   cin >> xs >> ys >> q;
   UnionFind uf(xs + ys);
   vector<pair<int, int> > p(q);
   for (int i = (0), _b = ((q)-1); i <= _b; i++) {
     int x, y;
     cin >> p[i].first >> p[i].second;
     --p[i].first;
     --p[i].second;
     uf.Union(p[i].first, xs + p[i].second);
   }
   map<int, SetInfo> ss;
   for (int i = (0), _b = ((q)-1); i <= _b; i++) {
     int id = uf.Find(p[i].first);
     SetInfo& setInfo = ss[id];
     setInfo._xc.insert(p[i].first);
     setInfo._yc.insert(p[i].second);
   }
   long long got = 0;
   long long totX = 0, totY = 0;
   for (auto& p : ss) {
     totX += (long long)p.second._xc.size();
     totY += (long long)p.second._yc.size();
   }
   long long remX = xs - totX;
   long long remY = ys - totY;
   long long res = remX + remY - 1 + ss.size();
   cout << res;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 T sqr(T x) {
   return x * x;
 }
 template <typename T>
 T abs(T x) {
   return x < 0 ? -x : x;
 }
 template <typename T>
 T gcd(T a, T b) {
   return b ? gcd(b, a % b) : a;
 }
 template <typename T>
 bool chmin(T& x, const T& y) {
   if (x > y) {
     x = y;
     return true;
   }
   return false;
 }
 template <typename T>
 bool chmax(T& x, const T& y) {
   if (x < y) {
     x = y;
     return true;
   }
   return false;
 }
 string to_bin(int x) {
   string ret;
   while (x) {
     ret = ret + (char)((x % 2) + '0');
     x /= 2;
   }
   return ret;
 }
 int main(int, char**) {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     long long n;
     cin >> n;
     long long m = (n - 1) / 3;
     int k = 0;
     long long p = 1;
     while (m >= p) {
       m -= p;
       k += 1;
       p *= 4;
     }
     vector<long long> a;
     for (int i = 0; i < k; ++i) {
       a.push_back(m % 4);
       m /= 4;
     }
     long long A, B;
     A = 1LL << (2 * k);
     B = 1LL << (2 * k + 1);
     vector<long long> b = {0, 2, 3, 1};
     for (int i = 0; i < k; ++i) {
       A ^= a[i] << (2 * i);
       B ^= b[a[i]] << (2 * i);
     }
     long long C = A ^ B;
     long long ans;
     if (n % 3 == 1) {
       ans = A;
     } else if (n % 3 == 2) {
       ans = B;
     } else {
       ans = C;
     }
     cout << ans << '
';
   }
   cerr << 'Time execute: ' << clock() / (double)CLOCKS_PER_SEC << ' sec'
        << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   double n, m, a, b;
   cin >> n >> m >> a >> b;
   double s = n / m;
   long long int s1 = floor(s) * m;
   long long int s2 = ceil(s) * m;
   long long int a1 = (n - s1) * b;
   long long int a2 = (s2 - n) * a;
   cout << (min(a1, a2));
 }