#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } struct mint { int x; mint() : x(0) {} mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} mint& operator+=(const mint& p){ if((x += p.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint& p){ if((x -= p.x) < 0) x += mod; return *this; } mint& operator*=(const mint& p){ x = (int)(1LL * x * p.x % mod); return *this; } mint& operator/=(const mint& p){ *this *= p.inverse(); return *this; } mint operator-() const { return mint(-x); } mint operator+(const mint& p) const { return mint(*this) += p; } mint operator-(const mint& p) const { return mint(*this) -= p; } mint operator*(const mint& p) const { return mint(*this) *= p; } mint operator/(const mint& p) const { return mint(*this) /= p; } bool operator==(const mint& p) const { return x == p.x; } bool operator!=(const mint& p) const { return x != p.x; } mint pow(int64_t n) const { mint res = 1, mul = x; while(n > 0){ if(n & 1) res *= mul; mul *= mul; n >>= 1; } return res; } mint inverse() const { return pow(mod - 2); } friend ostream& operator<<(ostream& os, const mint& p){ return os << p.x; } friend istream& operator>>(istream& is, mint& p){ int64_t val; is >> val; p = mint(val); return is; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; using edge = pair; vector> g(N + 1), rg(N + 1); for(int i = 0; i < M; ++i){ int u, v, l, a; cin >> u >> v >> l >> a; g[u].emplace_back(v, (ll)l * a); rg[v].emplace_back(u, (ll)l * a); } vector from0(N + 1), fromN(N + 1), visited(N + 1); auto dfs = [&](auto&& self, vector>& G, vector& f, int cur) -> void { visited[cur] = true; f[cur] = true; for(auto [nxt, cost] : G[cur]){ if(visited[nxt]) continue; self(self, G, f, nxt); } }; dfs(dfs, g, from0, 0); visited.assign(N + 1, false); dfs(dfs, rg, fromN, N); int V = 0; vector flag(N + 1); for(int i = 0; i <= N; ++i){ flag[i] = from0[i] && fromN[i]; V += flag[i]; } vector indeg(N + 1); for(int i = 0; i <= N; ++i){ if(!flag[i]) continue; for(auto [v, cost] : g[i]){ if(flag[i]) indeg[v] += 1; } } queue que; for(int i = 0; i <= N; ++i){ if(flag[i] && indeg[i] == 0){ que.emplace(i); } } vector topological_order; while(!que.empty()){ int from = que.front(); que.pop(); topological_order.emplace_back(from); for(auto [to, cost] : g[from]){ if(!flag[to]) continue; if(--indeg[to] == 0) que.emplace(to); } } if((int)topological_order.size() != V){ cout << "INF\n"; return 0; } vector dp(N + 1); for(int from : topological_order){ for(auto [to, cost] : g[from]){ if(!flag[to]) continue; dp[to] += dp[from] + cost; } } cout << dp[N] << endl; return 0; }