using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 7");
WillReturn.Add("1 2 2");
WillReturn.Add("1 4 1");
WillReturn.Add("2 3 7");
WillReturn.Add("1 5 12");
WillReturn.Add("3 5 2");
WillReturn.Add("2 5 3");
WillReturn.Add("3 4 5");
//13
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 4");
WillReturn.Add("1 2 1");
WillReturn.Add("1 3 1");
WillReturn.Add("1 4 1");
WillReturn.Add("1 5 1");
//-1
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 12");
WillReturn.Add("1 4 3");
WillReturn.Add("1 9 1");
WillReturn.Add("2 5 4");
WillReturn.Add("2 6 1");
WillReturn.Add("3 7 5");
WillReturn.Add("3 10 9");
WillReturn.Add("4 7 2");
WillReturn.Add("5 6 6");
WillReturn.Add("5 8 5");
WillReturn.Add("6 8 3");
WillReturn.Add("7 9 5");
WillReturn.Add("8 10 8");
//11
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
int N = wkArr[0];
int UB = N - 1;
int[,] KyoriArr1 = new int[UB + 1, UB + 1];
const int INFTY = int.MaxValue;
// 初期化処理
for (int I = 0; I <= UB; I++) {
for (int J = 0; J <= UB; J++) {
KyoriArr1[I, J] = (I == J) ? 0 : INFTY;
}
}
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
KyoriArr1[wkArr[0] - 1, wkArr[1] - 1] = wkArr[2];
KyoriArr1[wkArr[1] - 1, wkArr[0] - 1] = wkArr[2];
}
// ノード0との辺を切っておく
int[,] KyoriArr2 = (int[,])KyoriArr1.Clone();
for (int I = 0; I <= UB; I++) {
for (int J = 0; J <= UB; J++) {
if (I == 0 || J == 0) {
KyoriArr2[I, J] = INFTY;
}
}
}
// ワーシャルフロイド法
for (int K = 0; K <= UB; K++) {
for (int I = 0; I <= UB; I++) {
if (KyoriArr2[I, K] == INFTY) continue;
for (int J = 0; J <= UB; J++) {
if (KyoriArr2[K, J] == INFTY) continue;
int CurrKouho = KyoriArr2[I, K] + KyoriArr2[K, J];
if (KyoriArr2[I, J] > CurrKouho) {
KyoriArr2[I, J] = CurrKouho;
}
}
}
}
// ノード0と隣接する、2つのノードの組み合わせを全探索
int Answer = int.MaxValue;
bool FoundAnswer = false;
for (int I = 1; I <= UB; I++) {
if (KyoriArr1[0, I] == INFTY) continue;
for (int J = I + 1; J <= UB; J++) {
if (KyoriArr1[0, J] == INFTY) continue;
if (KyoriArr2[I, J] == INFTY) continue;
FoundAnswer = true;
int AnswerKouho = KyoriArr1[0, I] + KyoriArr1[0, J] + KyoriArr2[I, J];
Answer = Math.Min(Answer, AnswerKouho);
}
}
if (FoundAnswer) {
Console.WriteLine(Answer);
}
else {
Console.WriteLine(-1);
}
}
}