cin >> n >> m; for (int i = 1; i <= m; i++){ cin >> u >> v >> d1 >> d2; f[0][u][v] = d1; f[0][v][u] = d2; } for (int i = 1; i <= n; i++){ last[i][i] = 0; f[0][i][i] = 0; }
for (int l = 1; l <= 10; l++){ for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ f[l][i][j] = max(f[l][i][j], f[l - 1][i][k] + f[l - 1][k][j]); } } } } for (int l = 10; l >= 0; l--){ memset(tmp, -0x3f3f3f3f, sizeof(tmp));
for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ tmp[i][j] = max(tmp[i][j], last[i][k] + f[l][k][j]); } } }
bool flag = false; for (int i = 1; i <= n; i++){ if (tmp[i][i] > 0){ flag = true; break; } }
if (flag) continue; else{ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ last[i][j] = tmp[i][j]; } } ans += 1 << l; } } cout << (ans >= n ? 0 : ans + 1) << "\n"; return0; }