# include <iostream>
# include <vector>
# include <algorithm>
# include <cstring>
# include <iterator>
# include <string>
# include <unordered_map>
const int INF = 0x3f3f3f3f;
const int N = 100010;
const int M = 100010;
void solve()
{
/**** data ****/
int n, m;
std::cin >> n >> m;
std::vector<int> h(N, -1), e(N), ne(N), q(N), dist(N, INF), inque(N), w(N);
int idx = 0, ff = 0, rr = -1;
auto add = [&] (int a, int b, int c) -> void {
e[idx] = b, ne[idx] = h[a], h[a] = idx, w[idx] = c;
++idx;
};
for (int i = 0; i < m; ++ i) {
int x, y, z;
std::cin >> x >> y >> z;
add(x, y, z);
}
/**** core ****/
dist[1] = 0, q[++rr] = 1, inque[1] = 1;
while (ff <= rr) {
int t = q[ff ++];
inque[t] = 0;
for (int i = h[t]; i != -1; i = ne[i]) {
int v = e[i];
if (dist[t] + w[i] < dist[v]) {
dist[v] = dist[t] + w[i];
if (!inque[v]) {
inque[v] = 1;
q[++rr] = v;
}
}
}
}
if (dist[n] > INF/2) std::cout << "impossible" << std::endl;
else std::cout << dist[n] << std::endl;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int tt;
// std::cin >> tt;
tt = 1;
while (tt--) {
solve();
}
return 0;
}