using static IO;
public class IO
{
public static IO Cin = new();
public static StreamReader reader = new(Console.OpenStandardInput());
public static StreamWriter writer = new(Console.OpenStandardOutput());
public static implicit operator string(IO _) => reader.ReadLine();
public static implicit operator char[](IO _) => reader.ReadLine().ToArray();
public static implicit operator int(IO _) => int.Parse(reader.ReadLine());
public static implicit operator double(IO _) => double.Parse(reader.ReadLine());
public static implicit operator string[](IO _) => reader.ReadLine().Split();
public static implicit operator int[](IO _) => Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
public void Deconstruct(out int a, out int b) { int[] r = Cin; (a, b) = (r[0], r[1]); }
public void Deconstruct(out int a, out int b, out int c) { int[] r = Cin; (a, b, c) = (r[0], r[1], r[2]); }
public static IEnumerable<int> MakeRange(int start, int count) => Enumerable.Range(start, count);
public static T[] MakeArray<T>(int count) where T : new() => MakeRange(0, count).Select(_ => new T()).ToArray();
public static object? Cout { set { writer.Write(value); } }
public static object? Coutln { set { writer.WriteLine(value); } }
public static void Main() { Program.Coding(); writer.Flush(); }
}
class Program
{
public static void Coding()
{
checked
{
(int nodeCount, int lineCount) = Cin;
FlowGraph graph = new(nodeCount + 1);
for (int i = 0; i < lineCount; i++)
{
(int s, int e, int c) = Cin;
graph.AddLine(s, e, 1, c);
}
graph.AddLine(0, 1, 2, 0);
Cout = graph.GetMaxFlow(0, nodeCount, out int cost) == 2 ? cost : -1;
}
}
}
class FlowLine
{
public int Start { get; }
public int End { get; }
public int Capacity { get; }
public int Flow { get; set; } = 0;
public FlowLine Reverse { get; }
public int Remaining => Capacity - Flow;
public int Cost { get; }
public FlowLine(int start, int end, int capacity, int cost, FlowLine? reverse = null)
{
this.Start = start;
this.End = end;
this.Capacity = capacity;
this.Cost = cost;
this.Reverse = reverse ?? new(end, start, 0, -cost, this);
}
}
class FlowGraph
{
public FlowGraph(int size)
{
graph = MakeArray<List<FlowLine>>(Count = size);
}
public void AddLine(int start, int end, int capacity, int cost)
{
FlowLine line = new(start, end, capacity, cost);
graph[start].Add(line);
graph[end].Add(line.Reverse);
}
public int GetMaxFlow(int source, int sink, out int cost)
{
int totalFlow = 0;
for (cost = 0; true;)
{
FlowLine?[] path = new FlowLine?[Count];
int[] dist = Enumerable.Repeat(int.MaxValue, Count).ToArray();
bool[] inQueue = new bool[Count];
Queue<int> queue = new();
dist[source] = 0;
inQueue[source] = true;
queue.Enqueue(source);
while (queue.Count > 0)
{
int node = queue.Dequeue();
inQueue[node] = false;
foreach (var line in graph[node])
{
int nextdist = dist[node] + line.Cost;
if (line.Remaining > 0 && dist[line.End] > nextdist)
{
dist[line.End] = nextdist;
path[line.End] = line;
if (inQueue[line.End] is false)
{
inQueue[line.End] = true;
queue.Enqueue(line.End);
}
}
}
}
if (path[sink] is not FlowLine sinkLine) break;
int flow = int.MaxValue;
for (FlowLine? line = sinkLine; line is not null; line = path[line.Start])
{
flow = Math.Min(flow, line.Remaining);
}
for (FlowLine? line = sinkLine; line is not null; line = path[line.Start])
{
line.Flow += flow;
line.Reverse.Flow -= flow;
cost += flow * line.Cost;
}
totalFlow += flow;
}
return totalFlow;
}
public int Count { get; }
private readonly List<FlowLine>[] graph;
}