import java.io.*; import java.util.*; public class Main_yukicoder845 { private static Scanner sc; private static Printer pr; private static void solve() { int n = sc.nextInt(); int m = sc.nextInt(); FloydWarshall fw = new FloydWarshall(n); for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); if (fw.d[a][b] == fw.INF || c > fw.d[a][b]) { fw.addEdge(a, b, c); fw.addEdge(b, a, c); } } long max = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { long tmp = fw.getShortestPath(i, j); if (i != j && tmp != fw.INF) { max = Math.max(max, tmp); } } } pr.println(max); } // 全点対最短路 FloydWarshall O(V^3) private static class FloydWarshall { long[][] d; int n; long[][] result; boolean nf; // NEGATIVE CYCLE flag final long INF = Long.MAX_VALUE; FloydWarshall(int n) { this.n = n; d = new long[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(d[i], INF); d[i][i] = 0; } nf = false; } // i, j:0-indexed public void addEdge(int i, int j, int c) { d[i][j] = c; } // 負閉路があるときは-INFを返す public long getShortestPath(int i, int j) { if (nf) { return -INF; } if (result == null) { for (int kk = 0; kk < n; kk++) { for (int ii = 0; ii < n; ii++) { for (int jj = 0; jj < n; jj++) { // d[ii][jj] = Math.min(d[ii][jj], d[ii][kk] + d[kk][jj]); if (d[ii][kk] != INF && d[kk][jj] != INF && d[ii][jj] > d[ii][kk] + d[kk][jj]) { d[ii][jj] = d[ii][kk] + d[kk][jj]; } } } } for (int k = 0; k < n; k++) { if (d[k][k] < 0) { nf = true; return -INF; } } result = d; } return result[i][j]; } } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } } }