#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
const string INPUT_FILE = "dulich14.inp";
const string OUTPUT_FILE = "dulich14.out";
int N, M, minCost;
vector<vector<int>> adj(MAX_N + 1);
vector<vector<int>> weight(MAX_N + 1, vector<int>(MAX_N + 1, 0));
vector<bool> visited(MAX_N + 1, true);
vector<int> path(MAX_N + 1), bestPath(MAX_N + 1), cost(MAX_N + 1);
void addEdge(int u, int v, int c) {
adj[u].push_back(v);
adj[v].push_back(u);
weight[u][v] = c;
weight[v][u] = c;
}
void readInput() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int u, v, c;
cin >> u >> v >> c;
addEdge(u, v, c);
}
}
void updateBestPath() {
int totalCost = cost[N] + weight[path[N]][path[1]];
if (totalCost < minCost) {
minCost = totalCost;
bestPath = path;
}
}
void findHamiltonianCycle(int i) {
for (int j : adj[path[i - 1]]) {
if (visited[j] && weight[path[i - 1]][j] > 0) {
path[i] = j;
cost[i] = cost[i - 1] + weight[path[i - 1]][j];
if (cost[i] < minCost) {
if (i < N) {
visited[j] = false;
findHamiltonianCycle(i + 1);
visited[j] = true;
} else if (weight[j][path[1]] > 0) {
updateBestPath();
}
}
}
}
}
void printResult() {
if (minCost == INT_MAX) {
cout << "-1\n";
} else {
cout << minCost << "\n";
}
}
int main() {
freopen(INPUT_FILE.c_str(), "r", stdin);
freopen(OUTPUT_FILE.c_str(), "w", stdout);
readInput();
path[1] = 1;
cost[1] = 0;
visited[1] = false;
minCost = INT_MAX;
findHamiltonianCycle(2);
printResult();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTUFYX04gPSAxMDAwOwpjb25zdCBzdHJpbmcgSU5QVVRfRklMRSA9ICJkdWxpY2gxNC5pbnAiOwpjb25zdCBzdHJpbmcgT1VUUFVUX0ZJTEUgPSAiZHVsaWNoMTQub3V0IjsKCmludCBOLCBNLCBtaW5Db3N0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qOwogICAgICAgICAgICBjb3N0W2ldID0gY29zdFtpIC0gMV0gKyB3ZWlnaHRbcGF0aFtpIC0gMV1dW2pdOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYgKGNvc3RbaV0gPCBtaW5Db3N0KSB7CiAgICAgICAgICAgICAgICBpZiAoaSA8IE4pIHsKICAgICAgICAgICAgICAgICAgICB2aXNpdGVkW2pdID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgZmluZEhhbWlsdG9uaWFuQ3ljbGUoaSArIDEpOwogICAgICAgICAgICAgICAgICAgIHZpc2l0ZWRbal0gPSB0cnVlOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmICh3ZWlnaHRbal1bcGF0aFsxXV0gPiAwKSB7CiAgICAgICAgICAgICAgICAgICAgdXBkYXRlQmVzdFBhdGgoKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKdm9pZCBwcmludFJlc3VsdCgpIHsKICAgIGlmIChtaW5Db3N0ID09IElOVF9NQVgpIHsKICAgICAgICBjb3V0IDw8ICItMVxuIjsKICAgIH0gZWxzZSB7CiAgICAgICAgY291dCA8PCBtaW5Db3N0IDw8ICJcbiI7CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgZnJlb3BlbihJTlBVVF9GSUxFLmNfc3RyKCksICJyIiwgc3RkaW4pOwogICAgZnJlb3BlbihPVVRQVVRfRklMRS5jX3N0cigpLCAidyIsIHN0ZG91dCk7CiAgICAKICAgIHJlYWRJbnB1dCgpOwogICAgcGF0aFsxXSA9IDE7CiAgICBjb3N0WzFdID0gMDsKICAgIHZpc2l0ZWRbMV0gPSBmYWxzZTsKICAgIG1pbkNvc3QgPSBJTlRfTUFYOwogICAgCiAgICBmaW5kSGFtaWx0b25pYW5DeWNsZSgyKTsKICAgIHByaW50UmVzdWx0KCk7CiAgICAKICAgIHJldHVybiAwOwp9Cg==