#include <bits/stdc++.h>
using namespace std;
int dp[22][330][330], ans[330][330], temp[330][330];
int n, m;
bool judge(int a[][330]) {
  for (int i = 1; i <= n; ++i)
    if (a[i][i] > 0) return true;
  return false;
}
void floyd(int a[][330], int b[][330], int c[][330]) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) a[i][j] = -1e9;
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (a[i][j] < b[i][k] + c[k][j]) a[i][j] = b[i][k] + c[k][j];
}
int main() {
  while (~scanf("%d %d", &n, &m)) {
    int a, b, c1, c2;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (i == j)
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = -1e9;
    for (int i = 0; i < m; ++i) {
      scanf("%d %d %d %d", &a, &b, &c1, &c2);
      dp[0][a][b] = c1;
      dp[0][b][a] = c2;
    }
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) ans[i][j] = dp[0][i][j];
    for (int i = 0; i < 9; ++i) floyd(dp[i + 1], dp[i], dp[i]);
    if (!judge(dp[9])) {
      cout << 0 << endl;
      continue;
    }
    int num = 1;
    for (int i = 9; i >= 0; i--) {
      floyd(temp, dp[i], ans);
      if (!judge(temp)) {
        for (int j = 1; j <= n; ++j)
          for (int k = 1; k <= n; ++k) ans[j][k] = temp[j][k];
        num += (1 << i);
      }
    }
    cout << num + 1 << endl;
  }
}
