#include <bits/stdc++.h>
#define __QuocSensei__ int main()
#define Nap_card_20k ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define Co_tat_ca_nhung_thieu_em using namespace std
#define ii pair<int ,int>
Co_tat_ca_nhung_thieu_em;
const int maxn = 2e5 + 10;
int n, m, k = 2;
vector<ii> adj[maxn];
vector<int> dist[maxn];
void dijkstra(int s)
{
priority_queue<ii, vector<ii>, greater<ii>> pq;
pq.push({0, s});
while (!pq.empty())
{
int d = pq.top().first, u = pq.top().second;
pq.pop();
if (dist[u].size() == k) continue;
if (!dist[u].empty() && d == dist[u].back()) continue;
dist[u].push_back(d);
for (ii p : adj[u])
{
int w = p.second, v = p.first;
pq.push({d + w, v});
}
}
}
__QuocSensei__
{
Nap_card_20k;
if (fopen("SHORTEST.INP", "r"))
{
freopen("SHORTEST.INP", "r", stdin);
freopen("SHORTEST.OUT", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, w;
cin >> x >> y >> w;
adj[x].push_back({y, w});
}
dijkstra(1);
if (dist[n].size() == k) cout << dist[n][k-1];
else cout << -1;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIF9fUXVvY1NlbnNlaV9fIGludCBtYWluKCkKI2RlZmluZSBOYXBfY2FyZF8yMGsgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiNkZWZpbmUgQ29fdGF0X2NhX25odW5nX3RoaWV1X2VtIHVzaW5nIG5hbWVzcGFjZSBzdGQKI2RlZmluZSBpaSBwYWlyPGludCAsaW50PgoKQ29fdGF0X2NhX25odW5nX3RoaWV1X2VtOwoKY29uc3QgaW50IG1heG4gPSAyZTUgKyAxMDsKCmludCBuLCBtLCBrID0gMjsKdmVjdG9yPGlpPiBhZGpbbWF4bl07CnZlY3RvcjxpbnQ+IGRpc3RbbWF4bl07Cgp2b2lkIGRpamtzdHJhKGludCBzKQp7CiAgICBwcmlvcml0eV9xdWV1ZTxpaSwgdmVjdG9yPGlpPiwgZ3JlYXRlcjxpaT4+IHBxOwogICAgcHEucHVzaCh7MCwgc30pOwogICAgd2hpbGUgKCFwcS5lbXB0eSgpKQogICAgewogICAgICAgIGludCBkID0gcHEudG9wKCkuZmlyc3QsIHUgPSBwcS50b3AoKS5zZWNvbmQ7CiAgICAgICAgcHEucG9wKCk7CiAgICAgICAgaWYgKGRpc3RbdV0uc2l6ZSgpID09IGspIGNvbnRpbnVlOwogICAgICAgIGlmICghZGlzdFt1XS5lbXB0eSgpICYmIGQgPT0gZGlzdFt1XS5iYWNrKCkpIGNvbnRpbnVlOwogICAgICAgIGRpc3RbdV0ucHVzaF9iYWNrKGQpOwogICAgICAgIGZvciAoaWkgcCA6IGFkalt1XSkKICAgICAgICB7CiAgICAgICAgICAgIGludCB3ID0gcC5zZWNvbmQsIHYgPSBwLmZpcnN0OwogICAgICAgICAgICBwcS5wdXNoKHtkICsgdywgdn0pOwogICAgICAgIH0KICAgIH0KfQoKX19RdW9jU2Vuc2VpX18KewogICAgTmFwX2NhcmRfMjBrOwogICAgaWYgKGZvcGVuKCJTSE9SVEVTVC5JTlAiLCAiciIpKQogICAgewogICAgICAgIGZyZW9wZW4oIlNIT1JURVNULklOUCIsICJyIiwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4oIlNIT1JURVNULk9VVCIsICJ3Iiwgc3Rkb3V0KTsKICAgIH0KICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBtOyBpKyspCiAgICB7CiAgICAgICAgaW50IHgsIHksIHc7CiAgICAgICAgY2luID4+IHggPj4geSA+PiB3OwogICAgICAgIGFkalt4XS5wdXNoX2JhY2soe3ksIHd9KTsKICAgIH0KICAgIGRpamtzdHJhKDEpOwogICAgaWYgKGRpc3Rbbl0uc2l6ZSgpID09IGspIGNvdXQgPDwgZGlzdFtuXVtrLTFdOwogICAgZWxzZSBjb3V0IDw8IC0xOwp9Cg==