#include <bits/stdc++.h>   using namespace std;   const int MAXN = 310, INF = (1 << 27);   int d[MAXN][MAXN];   int all[20][MAXN][MAXN];   int ans[MAXN][MAXN];   int main() {     ios::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) d[i][j] = INF;     for (int i = 0; i < n; i++) d[i][i] = 0;     for (int i = 0; i < m; i++) {       int x, y, w1, w2;       cin >> x >> y >> w1 >> w2;       x--;       y--;       w1 = -w1;       w2 = -w2;       d[x][y] = w1;       d[y][x] = w2;     }     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) all[0][i][j] = ans[i][j] = d[i][j];     for (int k = 0; k < n; k++)       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);     bool hasNeg = false;     for (int i = 0; i < n; i++)       if (d[i][i] < 0) hasNeg = true;     if (!hasNeg) {       cout << 0 << endl;       return 0;     }     int ind;     for (ind = 1; (1 << ind) < n; ind++) {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) {           all[ind][i][j] = INF;           for (int k = 0; k < n; k++)             all[ind][i][j] =                 min(all[ind][i][j], all[ind - 1][i][k] + all[ind - 1][k][j]);         }     }     ind--;     int length = 1;     while (ind >= 0) {       int tmp[MAXN][MAXN];       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) {           tmp[i][j] = INF;           for (int k = 0; k < n; k++)             tmp[i][j] = min(tmp[i][j], ans[i][k] + all[ind][k][j]);         }       hasNeg = false;       for (int i = 0; i < n; i++)         if (tmp[i][i] < 0) hasNeg = true;       if (!hasNeg) {         for (int i = 0; i < n; i++)           for (int j = 0; j < n; j++) ans[i][j] = tmp[i][j];         length += (1 << ind);       }       ind--;     }     cout << length + 1 << endl;     return 0;   }   