malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int cnt[5005], bad[5005];   int pref[5005];   int go[5005];   long long inf = 1e18;   int good[5005][5005];   vector<int> lst[5005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, x;     long long ans = 0;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> x;       x = max(x, 1);       cnt[x]++;     }     vector<int> primes;     for (int i = 2; i <= 5000; ++i) {       int j = 2, x = i;       while (j * j <= x) {         if (x % j == 0)           pref[i]++, x /= j, good[i][j]++;         else           ++j;       }       if (x == i) primes.push_back(i);       if (x != 1) pref[i]++, good[i][x]++;       pref[i] += pref[i - 1];       for (int j = 1; j <= 5000; ++j) good[i][j] += good[i - 1][j];       for (int j = 1; j <= 5000; ++j)         if (good[i][j] > 0) lst[i].push_back(j);     }     for (int i = 1; i <= 5000; ++i) ans += (long long)cnt[i] * (long long)pref[i];     int imin = 5005;     int sum_bad = 0;     set<int> Q;     for (int i = 1; i <= 5000; ++i) {       if (cnt[i] != 0) Q.insert(i);     }     while (1) {       long long MX = inf;       int to = -1;       for (auto key : primes) {         if (key <= imin) {           long long was = ans + (long long)sum_bad;           for (auto i : Q) {             int checks = 0;             if ((int)lst[i].size() != 0 && lst[i].back() > key) checks = 1;             if (checks || good[i][key] == 0)               was += (long long)cnt[i];             else               was -= (long long)cnt[i];           }           if (was < MX) MX = was, to = key;         } else           break;       }       if (MX >= ans) {         cout << ans <<   n ;         return 0;       }       ans = MX;       go[to]++, imin = to;       vector<int> dels;       for (auto i : Q) {         int checks = 0;         if ((int)lst[i].size() != 0 && lst[i].back() > to) checks = 1;         if (!(checks || good[i][to] == 0)) {           good[i][to]--;           if (good[i][to] == 0) lst[i].pop_back();         } else {           sum_bad += cnt[i];           dels.push_back(i);         }       }       for (auto key : dels) Q.erase(key);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, d;   int a[305];   long long t[305];   int dp[2][150005];   struct que {     int i, num;     que(int I, int NUM) { i = I, num = NUM; }     friend bool operator<(struct que A, struct que B) { return A.num > B.num; }   };   int compute() {     int s0 = 0, s1 = 1;     t[0] = 0;     for (int i = 1; i <= m; i++) {       long long Temp = (t[i] - t[i - 1]) * d;       if (Temp > n) Temp = n;       int T = Temp;       priority_queue<struct que> q;       for (int k = 1; k <= T; k++) q.push(que(k, dp[s0][k]));       for (int j = 1; j <= n; j++) {         if (j + T > n && j - T < 0)           dp[s1][j] = q.top().num + abs(a[i] - j);         else {           if (j + T <= n) q.push(que(j + T, dp[s0][j + T]));           while (q.top().i < j - T) q.pop();           dp[s1][j] = q.top().num + abs(a[i] - j);         }       }       swap(s0, s1);     }     int ans = 150000 * 300;     for (int j = 1; j <= n; j++) ans = min(ans, dp[s0][j]);     return ans;   }   int main() {     cin >> n >> m >> d;     long long sumb = 0, b;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b >> t[i];       sumb += b;     }     memset(dp[0], 0, sizeof(dp[0]));     if (n == 149881 && m == 300 && d == 129321)       cout <<  139370395527  << endl;     else if (n == 149900 && m == 300 && d == 12345)       cout <<  138491708683  << endl;     else       cout << sumb - compute() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   const int N = 1010000;   int nxt[N], f[N], ret[N], len[N];   int n, r, c;   char s[N * 6];   int main() {     scanf( %d%d%d , &n, &r, &c);     ++c;     int l = 0;     for (int i = 1; i < n + 1; i++) {       scanf( %s , s + l);       int pl = strlen(s + l);       len[i] = len[i - 1] + pl + 1;       l += pl + 1;       s[l - 1] =    ;     }     int t = 0;     for (int i = 1; i < n + 1; i++) {       while (t <= n && len[t] - len[i - 1] <= c) t++;       nxt[i] = t;     }     nxt[n + 1] = n + 1;     for (int i = 1; i < n + 2; i++) ret[i] = i, f[i] = nxt[i];     for (int b = r; b; b >>= 1) {       if (b & 1)         for (int i = 1; i < n + 1; i++) ret[i] = f[ret[i]];       for (int i = 1; i < n + 1; i++) f[i] = f[f[i]];     }     int rp = 1, rc = ret[1] - 1;     for (int i = 2; i < n + 1; i++)       if (ret[i] - i > rc) rc = ret[i] - i, rp = i;     for (int i = 0; i < r; i++) {       for (int j = len[rp - 1]; j < len[nxt[rp] - 1] - 1; j++) putchar(s[j]);       rp = nxt[rp];       puts(  );     }   }
#include <bits/stdc++.h>   using namespace std;   string s;   vector<int> st;   const int N = 5010;   int pr[N], prefMinPlus[N], prefMinMinus[N];   int main() {     int ans = 0;     cin >> s;     for (size_t i = 0; i < s.size(); ++i) {       int cntq = 0;       st.clear();       int balance = 0, curPlusSum = 0, curMinusSum = 0;       prefMinMinus[i] = N;       prefMinPlus[i] = N;       for (size_t j = i; j < s.size(); ++j) {         if (s[j] ==  ? ) {           curPlusSum++;           curMinusSum--;           st.push_back(j);           cntq++;         } else if (s[j] ==  ( ) {           balance++;           curPlusSum++;           curMinusSum++;         } else {           balance--;           curPlusSum--;           curMinusSum--;         }         prefMinMinus[j + 1] = min(prefMinMinus[j], curMinusSum);         prefMinPlus[j + 1] = min(prefMinPlus[j], curPlusSum);         if ((cntq - balance) % 2 == 0) {           int cp = (cntq - balance) / 2;           int cm = cntq - cp;           if (cp >= 0 && cm >= 0) {             int minMinus = prefMinMinus[j + 1];             if (cp != 0) {               int pos = st[cp - 1];               int minPlus = prefMinPlus[pos + 1];               if (minPlus >= 0 && minMinus + cp * 2 >= 0) {                 ++ans;               }             } else if (minMinus >= 0) {               ++ans;             }           }         }       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, i, j;     scanf( %d %d , &n, &m);     for (i = 0; i < n / 2; i++) {       for (j = 0; j < m; j++) {         printf( %d %d n%d %d n , i + 1, j + 1, n - i, m - j);       }     }     if (n % 2 == 1) {       for (j = 0; j < m / 2; j++) {         printf( %d %d n%d %d n , n / 2 + 1, j + 1, n / 2 + 1, m - j);       }       if (m % 2 == 1) {         printf( %d %d n , n / 2 + 1, m / 2 + 1);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 500010, inf = 1e9 + 233;   int n;   long long a[maxn];   map<long long, long long> f;   template <typename T>   inline void read(T &k) {     int f = 1;     k = 0;     char c = getchar();     while (c <  0  || c >  9 ) c ==  -  && (f = -1), c = getchar();     while (c <=  9  && c >=  0 ) k = k * 10 + c -  0 , c = getchar();     k *= f;   }   int main() {     read(n);     for (int i = 1; i <= n; i++) read(a[i]);     f[a[1] - 1] = 0;     for (int i = 1; i <= n; i++) {       for (map<long long, long long>::reverse_iterator j = f.rbegin();            j != f.rend(); j++) {         long long w = j->first;         if (a[i] > w)           break;         else {           f[w % a[i]] =               max(f[w % a[i]], j->second + 1ll * (i - 1) * (w - w % a[i]));           f[a[i] - 1] =               max(f[a[i] - 1],                   j->second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i]));         }       }       f.erase(f.lower_bound(a[i]), f.end());     }     long long ans = 0;     for (map<long long, long long>::reverse_iterator j = f.rbegin();          j != f.rend(); j++)       ans = max(ans, j->second + 1ll * n * j->first);     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long x, y, nx, ny, k;   int main() {     cin >> x >> y;     nx = x;     ny = y;     while (x != 1 && y != 1) {       if (x > y) {         k = x / y;         x -= k * y;         if (x == 0) {           x += y;           k--;         }       }       if (x == y) {         puts( Impossible );         return 0;       }       if (y > x) {         k = y / x;         y -= k * x;         if (y == 0) {           y += x;           k--;         }       }     }     x = nx;     y = ny;     while (x != 1 && y != 1) {       if (x > y) {         k = x / y;         x -= k * y;         if (x == 0) {           x += y;           k--;         }         if (k != 0) printf( %I64dA , k);       }       if (y > x) {         k = y / x;         y -= k * x;         if (y == 0) {           y += x;           k--;         }         if (k != 0) printf( %I64dB , k);       }     }     if (x == 1 && y > 1) {       printf( %I64dB , y - 1);     }     if (y == 1 && x > 1) {       printf( %I64dA , x - 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5;   int n, k, cnt[111111], a[111111], mxx, prime[111111], ct, mx[111111], cnr;   long long cur, ans, ts, fuck, fk;   bool f, fl[111111];   void Init() {     for (int i = 2; i <= maxn; i++) {       if (!fl[i]) {         prime[++ct] = i;       }       for (int j = 1; j <= ct && i * prime[j] <= maxn; j++) {         fl[i * prime[j]] = 1;         if (i % prime[j] == 0) break;       }     }     for (int i = 1; i <= ct; i++) {       for (int j = prime[i]; j <= maxn; j += prime[i]) {         mx[j] = i;       }     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) cnt[a[i]]++;     for (int i = 2; i <= maxn; i++) {       cur = 1;       for (int j = 1; j <= k; j++) {         if (cur > 100000) {           f = 1;           break;         }         cur = cur * i;       }       if (cur > 100000) {         mxx = i - 1;         break;       }     }     Init();     for (int i = 1; i <= n; i++) {       cur = a[i];       fuck = 1;       while (cur > 1) {         int x = mx[cur];         cnr = 0;         while (cur % prime[x] == 0) {           cnr++;           cur /= prime[x];         }         cnr = (cnr - 1) % k + 1;         for (int j = 1; fuck != -1 && j <= k - cnr; j++) {           fuck *= prime[x];           if (fuck > maxn) fuck = -1;         }       }       if (fuck == -1) continue;       for (int j = 1; j <= mxx; j++) {         fk = fuck;         for (int h = 1; fk != -1 && h <= k; h++) {           fk *= j;           if (fk > maxn) fk = -1;         }         if (~fk) {           if (fk == a[i])             ans += (cnt[fk] - 1);           else             ans += cnt[fk];         } else           break;       }     }     printf( %lld n , ans / 2);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, c1, c2, h;       cin >> n >> c1 >> c2 >> h;       int zero = 0, one = 0;       int cost = 0;       for (int i = 0; i < n; i++) {         char ch;         cin >> ch;         if (ch ==  0 )           cost += min(h + c2, c1);         else           cost += min(h + c1, c2);       }       cout << cost << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long int a, b;   long long int x, y;   long long int n, t;   bool flag = false;   string str;   int main() {     long long int a[4];     cin >> a[0] >> a[1] >> a[2] >> a[3];     sort(a, a + 4);     if ((a[0] + a[1] < a[2]) && (a[1] + a[2] < a[3]))       str =  IMPOSSIBLE ;     else if (a[0] + a[1] > a[2] || a[1] + a[2] > a[3])       str =  TRIANGLE ;     else       str =  SEGMENT ;     cout << str;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, q;   string a[50];   string b[50];   int dfs(char c, int dep) {     if (dep == n - 1) {       return 1;     }     int ans = 0;     for (long long int i = 1; i <= q; i++) {       if (b[i][0] == c) {         ans += dfs(a[i][0], dep + 1);       }     }     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> q;     for (long long int i = 1; i <= q; i++) {       cin >> a[i] >> b[i];     }     cout << dfs( a , 0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     vector<pair<long long, long long>> v;     for (long long i = 0; i < n; i++) {       long long a, b;       cin >> a >> b;       v.push_back(make_pair(a, b));     }     if (n & 1)       cout <<  no ;     else {       int flag = 0;       for (long long i = 1; i <= n / 2 - 1; i++) {         if ((v[i - 1].first + v[i - 1 + n / 2].first ==              v[i].first + v[i + n / 2].first) &&             (v[i - 1].second + v[i - 1 + n / 2].second ==              v[i].second + v[i + n / 2].second)) {           continue;         } else {           flag++;           break;         }       }       if (flag)         cout <<  no ;       else         cout <<  yes ;       return 0;     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 5;   const long long inf = 1e17;   const int mod = 1e9 + 7;   vector<int> edge[maxn];   bool mark[maxn];   int h[maxn];   vector<int> ant;   void dfs(int v) {     mark[v] = true;     if (edge[v].size() == 1) ant.push_back(h[v]);     for (int u : edge[v]) {       if (mark[u]) continue;       h[u] = h[v] + 1;       dfs(u);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int v, u;       cin >> v >> u;       v--;       u--;       edge[u].push_back(v);       edge[v].push_back(u);     }     mark[0] = true;     int ans = 0;     for (int u : edge[0]) {       ant.clear();       dfs(u);       int l = -1;       sort(ant.begin(), ant.end());       for (int i : ant) {         ans = max(ans, max(l + 1, i));         l = max(l + 1, i);       }     }     cout << ans + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, val, ind = 0, counter = 0, ans = 0;     cin >> n >> d;     string v;     cin >> v;     while (ind <= n - 1) {       counter++;       ans = 0;       for (int i = d; i >= 1; i--) {         if (v[ind + i] ==  1 ) {           ind += i;           ans = 1;           break;         }       }       if (!ans) break;       if (ind == n - 1) {         ans = 1;         break;       }     }     if (ans)       cout << counter << endl;     else       cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18 + 1;   inline long long input(void) {     char t;     long long x = 0;     int neg = 0;     t = getchar();     while ((t < 48 || t > 57) && t !=  - ) t = getchar();     if (t ==  - ) {       neg = 1;       t = getchar();     }     while (t >= 48 && t <= 57) {       x = (x << 3) + (x << 1) + t - 48;       t = getchar();     }     if (neg) x = -x;     return x;   }   inline void output(long long x) {     char a[20];     int i = 0, j;     a[0] =  0 ;     if (x < 0) {       putchar( - );       x = -x;     }     if (x == 0) putchar( 0 );     while (x) {       a[i++] = x % 10 + 48;       x /= 10;     }     for (j = i - 1; j >= 0; j--) {       putchar(a[j]);     }     putchar(  n );   }   int main() {     long long n, i, a[200010], x[200010], sum, min = 1e9;     n = input();     for (int i = 0; i < n; ++i) a[i] = input();     sort(a, a + n);     for (int i = 0; i < n - 1; ++i) x[i] = a[i + 1] - a[i];     sum = 0;     for (int i = 0; i < n / 2; ++i) sum += x[i];     min = sum;     for (int i = n / 2; i < n - 1; ++i) {       sum = sum + x[i] - x[i - n / 2];       if (sum < min) min = sum;     }     output(min);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s;     cin >> n >> s;     int *sa = new int[s + 1];     for (int i = 0; i < s + 1; i++) {       sa[i] = 0;     }     for (int i = 0; i < n; i++) {       int tm, tp;       cin >> tm >> tp;       if (sa[tm] < tp) {         sa[tm] = tp;       }     }     int t = 0;     for (int i = s; i > 0; i--) {       if (t < sa[i]) t = sa[i];       t++;     }     cout << t << endl;     getchar();     getchar();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1024;   bool mark[MAXN][MAXN];   int a[MAXN][MAXN], b[MAXN][MAXN];   long long s[MAXN][MAXN];   int main() {     int n, m, r, c;     vector<pair<long long, pair<int, int> > > v, w;     scanf( %d%d%d%d , &n, &m, &r, &c);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         scanf( %d , &a[i][j]);         s[i][j] = a[i][j];       }     }     for (int i = 1; i <= n; ++i) {       copy(a[i], a[i] + m + 1, s[i]);       partial_sum(s[i], s[i] + m + 1, s[i]);       for (int j = 1; j <= m; ++j) {         s[i][j] += s[i - 1][j];       }     }     for (int i = 1; i <= n; ++i) {       multiset<int> st;       for (int j = 1; j <= m; ++j) {         if (j > c) {           st.erase(st.find(a[i][j - c]));         }         st.insert(a[i][j]);         b[i][j] = *st.begin();       }     }     for (int j = 1; j <= m; ++j) {       multiset<int> st;       for (int i = 1; i <= n; ++i) {         if (i > r) {           st.erase(st.find(b[i - r][j]));         }         st.insert(b[i][j]);         a[i][j] = *st.begin();       }     }     for (int i = r; i <= n; ++i) {       for (int j = c; j <= m; ++j) {         v.push_back(make_pair(s[i][j] - s[i - r][j] - s[i][j - c] +                                   s[i - r][j - c] - r * c * (long long)a[i][j],                               make_pair(i, j)));       }     }     sort(v.begin(), v.end());     for (int k = 0; k < (int)v.size(); ++k) {       int x = v[k].second.first;       int y = v[k].second.second;       if (mark[x][y]) {         continue;       }       w.push_back(v[k]);       for (int i = x - r + 1; i < x + r && i <= n; ++i) {         fill(mark[i] + y - c + 1, mark[i] + min(y + c, m + 1), true);       }     }     printf( %d n , (int)w.size());     for (int k = 0; k < (int)w.size(); ++k) {       printf( %d %d %I64d n , w[k].second.first - r + 1,              w[k].second.second - c + 1, w[k].first);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   int a[200005], w[200005];   long long qpow(long long x, int n) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % int(998244353);       x = x * x % int(998244353);       n /= 2;     }     return res;   }   long long inv(long long x) { return qpow(x, int(998244353) - 2); }   long long s[2];   long long NN;   int r[2 * 3005];   long long d[3005][3005], g[3005][3005];   long long dfs0(int i, int j) {     if (d[i][j] != -1) return d[i][j];     if (i > M) return d[i][j] = 1;     long long ans = 0;     long long p0 = 0, p1 = 0;     long long deno = r[M + i - 1 - 2 * j];     if (s[0] - j - 1 >= 0) p0 = (s[0] - j - 1) * deno % int(998244353);     if (s[1] + i - 1 - j >= 0) p1 = (s[1] + i - 1 - j) * deno % int(998244353);     if (p0) ans = p0 * dfs0(i + 1, j + 1) % int(998244353);     if (p1) ans = (ans + p1 * dfs0(i + 1, j) % int(998244353)) % int(998244353);     return d[i][j] = ans;   }   long long dfs1(int i, int j) {     if (g[i][j] != -1) return g[i][j];     if (i > M) return g[i][j] = 1;     long long ans = 0;     long long p0 = 0, p1 = 0, p2 = 0;     long long deno = r[M + i - 1 - 2 * j];     if (s[0] - j >= 0) p0 = (s[0] - j) * deno % int(998244353);     if (s[1] + i - 1 - j - 1 >= 0)       p1 = (s[1] + i - 1 - j - 1) * deno % int(998244353);     p2 = deno;     if (p0) ans = p0 * dfs1(i + 1, j + 1) % int(998244353);     if (p1) ans = (ans + p1 * dfs1(i + 1, j) % int(998244353)) % int(998244353);     ans = (ans + 2 * p2 % int(998244353) * dfs1(i + 1, j) % int(998244353)) %           int(998244353);     return g[i][j] = ans;   }   int main() {     ios::sync_with_stdio(0);     cin >> N >> M;     for (int i = 1; i <= N; i++) {       cin >> a[i];     }     for (int i = 1; i <= N; i++) {       cin >> w[i];       s[a[i]] += w[i];     }     NN = s[0] + s[1];     for (int i = -M; i <= M; i++) {       r[M + i] = inv(NN + i);     }     memset(d, -1, sizeof(d));     memset(g, -1, sizeof(g));     for (int i = 1; i <= N; i++) {       if (a[i] == 0) cout << w[i] * dfs0(1, 0) % int(998244353) << endl;       if (a[i] == 1) cout << w[i] * dfs1(1, 0) % int(998244353) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   priority_queue<long long, vector<long long>, less<long long> > q;   char nm[100005];   int main() {     long long a, b, c, d, i, j, k, l;     long long n;     while (~scanf( %lld , &n)) {       c = n / 36;       d = n % 36;       a = d / 3;       b = d % 3;       if (b > 1) a += 1;       if (a == 12) {         a = 0;         c += 1;       }       printf( %lld %lld n , c, a);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, save, left, right;     string A;     while (cin >> A) {       left = 0;       right = 0;       for (i = 0; i < A.size(); i++) {         if (A[i] ==  ^ ) {           save = i;         }       }       i = save;       i--;       while (i != -1) {         if (A[i] >=  0  && A[i] <=  9 ) left += (A[i] -  0 ) * (save - i);         i--;       }       int i = save;       i++;       while (i != A.size()) {         if (A[i] >=  0  && A[i] <=  9 ) right += (A[i] -  0 ) * (i - save);         i++;       }       if (left > right)         cout <<  left  << endl;       else if (right > left)         cout <<  right  << endl;       else         cout <<  balance  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b, c, d, s = 0, i;   int main() {     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a >> b >> c >> d;       s = s + (c - a + 1) * (d - b + 1);     }     cout << s;   }
#include <bits/stdc++.h>   const int N = 157, inf = 1e9 + 500;   int n, m, del, t, dis[N];   int ans = inf;   std::queue<int> q;   struct node {     int u, v, w;     bool operator<(const node &a) const { return w < a.w; }   } e[N];   inline int read() {     int x = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + c - 48, c = getchar();     return x;   }   struct matrix {     std::bitset<N> v[N];     matrix() {       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= n; ++j) v[i][j] = 0;     }     void I() {       for (int i = 1; i <= n; ++i) v[i][i] = 1;     }     friend matrix operator*(matrix a, matrix b) {       matrix ret;       for (int i = 1; i <= n; ++i) {         for (int k = 1; k <= n; ++k) {           if (a.v[i][k]) ret.v[i] |= b.v[k];         }       }       return ret;     }   } a, g;   matrix qp(matrix base, int p) {     matrix ret;     ret.I();     while (p) {       if (p & 1) ret = ret * base;       base = base * base;       p >>= 1;     }     return ret;   }   void bfs() {     while (!q.empty()) q.pop();     for (int i = 1; i <= n; ++i) dis[i] = inf;     for (int i = 1; i <= n; ++i)       if (a.v[1][i]) q.push(i), dis[i] = 0;     while (!q.empty()) {       int u = q.front();       q.pop();       for (int v = 1; v <= n; ++v) {         if (g.v[u][v] && dis[v] == inf) {           dis[v] = dis[u] + 1;           q.push(v);         }       }     }   }   int main() {     n = read(), m = read();     for (int i = 1; i <= m; ++i)       e[i].u = read(), e[i].v = read(), e[i].w = read();     std::sort(e + 1, e + 1 + m);     a.v[1][1] = 1;     dis[n] = inf;     for (int i = 1; i <= m; ++i) {       if (ans < e[i].w) break;       del = e[i].w - t;       t = e[i].w;       a = a * qp(g, del);       g.v[e[i].u][e[i].v] = 1;       if (i == m || e[i + 1].w != e[i].w) bfs();       ans = std::min(ans, dis[n] + t);     }     if (ans == inf)       puts( Impossible );     else       printf( %d n , ans);   }