#include using namespace std; using ll = long long; using pll = pair; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template struct edge{ int from = -1; int to = -1; T cost = T(1); int id = -1; edge() = default; edge(int to, T cost=T(1)) : to(to), cost(cost){} edge(int from, int to, T cost, int id=-1) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} edge rev() const { return edge(to, from, cost, id); } }; template struct edges : std::vector>{ using std::vector>::vector; void sort_by_cost(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } void rsort_by_cost(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost > b.cost; } ); } void sort(){ sort_by_cost(); } }; template struct graph{ private: int n = 0; int m = 0; vector> adj; edges es; bool dir = false; public: graph() = default; graph(int n, bool dir=false) : n(n), adj(n), dir(dir){} int add_vertex(){ adj.push_back(edges()); return n++; } int add_edge(int from, int to, T cost=T(1)){ int id = m++; es.push_back(edge(from, to, cost, id)); if(dir){ adj[from].push_back(edge(from, to, cost, id)); }else{ adj[from].push_back(edge(from, to, cost, id)); adj[to].push_back(edge(to, from, cost, id)); } return id; } int size() const{ return n; } bool empty() const{ return n == 0; } edges& operator[](int v){ return adj[v]; } const edges& operator[](int v) const{ return adj[v]; } auto begin(){ return adj.begin(); } auto end(){ return adj.end(); } auto begin() const{ return adj.begin(); } auto end() const{ return adj.end(); } int edge_size() const{ return m; } bool get_dir() const{ return dir; } edge get_edge(int i) const{ return es[i]; } const edges& get_edge_set() const{ return es; } const edges& get_edges() const{ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct shortest_path_tree{ private: const T TINF = numeric_limits::max()/3; int n, s; graph G; vector dist; vector vpar; edges epar; public: shortest_path_tree(graph G, int s) : G(G), s(s){ n = G.size(); dist.resize(n, TINF); vpar.resize(n, -1); epar.resize(n); run(); } void run(){ dist[s] = 0; priority_queue, vector>, greater<>> que; que.push({0, s}); while(!que.empty()){ auto [d, v] = que.top(); que.pop(); if(dist[v] < d) continue; for(auto e : G[v]){ if(dist[v] + e.cost < dist[e.to]){ dist[e.to] = dist[v] + e.cost; vpar[e.to] = v; epar[e.to] = e; que.push({dist[e.to], e.to}); } } } } T get_dist(int t){ return dist[t]; } vector get_dist(){ return dist; } vector get_vpar(){ return vpar; } int get_vpar(int v){ return vpar[v]; } edges get_epar(){ return epar; } edge get_epar(int v){ return epar[v]; } vector get_vpath(int t){ vector vpath; int cur = t; while(cur != -1){ vpath.push_back(cur); cur = vpar[cur]; } reverse(vpath.begin(), vpath.end()); return vpath; } edges get_epath(int t){ edges epath; int cur = t; while(cur != s){ epath.push_back(epar[cur]); cur = vpar[cur]; } reverse(epath.begin(), epath.end()); return epath; } graph get_tree(){ graph spt(n, false); for(int v=0; v get_shotest_path_tree(){ return get_tree(); } }; template edges min_weight_cycle(graph &G, int s){ int n = G.size(); const S SINF = numeric_limits::max()/3; bool dir = G.get_dir(); shortest_path_tree dijk(G, s); auto dist = dijk.get_dist(); edges cyc; if(dir){ S cost = SINF; edge emin; for(int v=0; v> ch(n); for(int v=0; v label(n, -1); label[s] = s; function labeling = [&](int v, int l){ label[v] = l; for(int to : ch[v]) labeling(to, l); }; for(int to : ch[s]) labeling(to, to); S cost = SINF; edge emin; for(int v=0; v edges min_weight_cycle(graph &G){ int n = G.size(); const S SINF = numeric_limits::max()/2; S cost = SINF; edges min_cyc; for(int s=0; s> T; int n, m; cin >> n >> m; edges cyc; if(T == 0){ graph G(n, false); for(int i=0; i> u >> v >> w; G.add_edge(u-1, v-1, w); } cyc = min_weight_cycle(G); } if(T == 1){ graph G(n, true); for(int i=0; i> u >> v >> w; G.add_edge(u-1, v-1, w); } cyc = min_weight_cycle(G); } if(cyc.empty()){ cout << -1 << '\n'; return 0; } ll ans = 0LL; for(auto e : cyc) ans += e.cost; cout << ans << '\n'; return 0; }