#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; template CostType girth_in_directed_graph(const std::vector>> &graph, const CostType CINF) { int n = graph.size(); CostType res = CINF; std::vector dist(n); using Pci = std::pair; std::priority_queue, std::greater> que; for (int root = 0; root < n; ++root) { std::fill(dist.begin(), dist.end(), CINF); dist[root] = 0; que.emplace(0, root); while (!que.empty()) { CostType cost; int ver; std::tie(cost, ver) = que.top(); que.pop(); if (dist[ver] < cost) continue; for (const Edge &e : graph[ver]) { CostType cost = dist[ver] + e.cost; if (cost < dist[e.dst]) { dist[e.dst] = cost; que.emplace(cost, e.dst); } else if (e.dst == root) { if (cost < res) res = cost; } } } } return res; } template CostType girth_in_undirected_graph(int n, const std::vector> &edges, const CostType CINF) { int m = edges.size(); std::vector> graph(n); for (int i = 0; i < m; ++i) { graph[edges[i].src].emplace_back(i); graph[edges[i].dst].emplace_back(i); } std::vector used(m, false); std::vector dist(n); std::vector label(n), prev(n); using Pci = std::pair; std::priority_queue, std::greater> que; CostType res = CINF; for (int root = 0; root < n; ++root) { std::fill(used.begin(), used.end(), false); std::fill(dist.begin(), dist.end(), CINF); dist[root] = 0; std::fill(label.begin(), label.end(), -2); label[root] = -1; std::fill(prev.begin(), prev.end(), -1); que.emplace(0, root); while (!que.empty()) { CostType c; int ver; std::tie(c, ver) = que.top(); que.pop(); if (dist[ver] < c) continue; for (int id : graph[ver]) { int dst = edges[id].src == ver ? edges[id].dst : edges[id].src; CostType cost = dist[ver] + edges[id].cost; if (cost < dist[dst]) { dist[dst] = cost; label[dst] = label[ver] == -1 ? dst : label[ver]; if (prev[dst] != -1) used[dst] = true; used[id] = true; que.emplace(cost, dst); } } } for (int i = 0; i < m; ++i) { int src = edges[i].src, dst = edges[i].dst; CostType cost = edges[i].cost; if (!used[i] && label[src] != -2 && label[dst] != -2) { if (label[src] != label[dst]) { CostType loop = dist[src] + dist[dst] + cost; if (loop < res) res = loop; } else if (label[src] == -1) { if (cost < res) res = cost; } } } } return res; } int main() { int t, n, m; cin >> t >> n >> m; if (t == 0) { vector> edges; while (m--) { int u, v, w; cin >> u >> v >> w; --u; --v; edges.emplace_back(u, v, w); } ll ans = girth_in_undirected_graph(n, edges, LINF); cout << (ans == LINF ? -1 : ans) << '\n'; } else if (t == 1) { vector>> graph(n); while (m--) { int u, v, w; cin >> u >> v >> w; --u; --v; graph[u].emplace_back(u, v, w); } ll ans = girth_in_directed_graph(graph, LINF); cout << (ans == LINF ? -1 : ans) << '\n'; } return 0; }