#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <class T>
bool ckmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = 0;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
const int N = 155;
const int inf = 0x3f3f3f3f;
int n, m, ans = inf;
struct road {
  int x, y, d;
} a[N];
inline bool cmp(const road &a, const road &b) { return a.d < b.d; }
struct Matrix {
  bitset<N> a[N];
  Matrix() {
    for (int i = 1, iend = n; i <= iend; ++i) a[i].reset();
  }
  friend Matrix operator*(const Matrix &a, const Matrix &b) {
    Matrix res;
    for (int i = 1, iend = n; i <= iend; ++i)
      for (int k = 1, kend = n; k <= kend; ++k)
        if (a.a[i][k]) res.a[i] |= b.a[k];
    return res;
  }
};
inline Matrix Matrix_qpow(Matrix a, int k) {
  Matrix res = a;
  for (--k; k; k >>= 1, a = a * a)
    if (k & 1) res = res * a;
  return res;
}
signed main() {
  n = read(), m = read();
  for (int i = 1, iend = m; i <= iend; ++i)
    a[i].x = read(), a[i].y = read(), a[i].d = read();
  sort(a + 1, a + m + 1, cmp);
  if (a[1].d != 0) return puts("Impossible"), 0;
  Matrix A, B;
  A.a[1][1] = 1;
  for (int i = 1, j = 1; i <= m; ++i) {
    static int dis[N];
    j = i;
    while (j + 1 <= m && a[j + 1].d == a[i].d) ++j;
    for (int k = i, kend = j; k <= kend; ++k) B.a[a[k].x][a[k].y] = 1;
    i = j;
    queue<int> q;
    memset(dis, 0x3f, sizeof(dis));
    for (int k = 1, kend = n; k <= kend; ++k)
      if (A.a[1][k]) q.push(k), dis[k] = a[i].d;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int v = 1, vend = n; v <= vend; ++v)
        if (B.a[u][v] && ckmin(dis[v], dis[u] + 1)) q.push(v);
    }
    ckmin(ans, dis[n]);
    if (j < m) A = A * Matrix_qpow(B, a[j + 1].d - a[j].d);
  }
  if (ans == inf)
    puts("Impossible");
  else
    cout << ans << '\n';
}
