#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 101;

struct edge
{
    int to,cost;
};

vector<edge> G[MAX_N];
int d[MAX_N];

class Simplex {
private:
    using Arr = vector<double>;
    using Mat = vector<vector<double> >;
    bool LT(double a, double y){
        return y-a > EPS;
    }
    bool EQ(double a, double y){
        return abs(a-y) < EPS;
    }
    bool LE(double a, double y){
        return y-a > -EPS;
    }
    vector<int> index;
    Mat a;
    int row,column,L;

    void Set(const Mat& A, const Arr& b, const Arr& c){
        infinity = none = false;
        row = A.size(),column = A[0].size() + 1;
        index.resize(row+column);
    	rep(i,row+column){
            index[i] = i;
        }
        L = row;
        a.resize(row+2,Arr(column+1,0));
        rep(i,row){
    	    rep(j,column-1){
                a[i][j] = -A[i][j];
            }
    	    a[i][column-1] = 1;
    	    a[i][column] = b[i];
    	    if(a[L][column] > a[i][column]){
                L = i;
            }
    	}
    	rep(j,column-1){
            a[row][j] = c[j];
        }
    	a[row+1][column-1] = -1;
    }

    void solve(Mat& a){
        for(int E = column-1;;){
    	    if(L < row){
                swap(index[E],index[L+column]);
        		a[L][E] = 1 / a[L][E];
                rep(j,column+1){
                    if(j != E){
                        a[L][j] *= -a[L][E];
                    }
                }
        		rep(i,row+2){
                    if(i != L){
                        rep(j,column+1){
                            if(j != E){
                                a[i][j] += a[i][E] * a[L][j];
                            }
                        }
            		    a[i][E] = a[i][E] * a[L][E];
                    }
        		}
    	    }
    	    E = -1;
    	    rep(j,column){
                if(E < 0 || index[E] > index[j]){
                    if(LT(0,a[row+1][j]) || (EQ(a[row+1][j],0) && LT(0,a[row][j]))){
                        E = j;
                    }
                }
            }
    	    if(E < 0) break;
            L = -1;
            rep(i,row){
                if(LT(a[i][E], 0)){
                    if(L < 0 || LE(a[L][column] / a[L][E], a[i][column] / a[i][E])){
                        L = i;
                    }
                }
            }
            if(L < 0){
                infinity = true;
                return;
            }
    	}
    	if(LT(a[row+1][column], 0)){
    	    none = true;
    	    return;
    	}
    	x.assign(column-1, 0);
    	rep(i,row){
            if(index[column+i] < column-1){
                x[index[column+i]] = a[i][column];
            }
        }
    	ans = a[row][column];
    }
public:
    bool infinity, none;
    double ans;
    Arr x;
    Simplex(const Mat& A, const Arr& b, const Arr& c){
        Set(A,b,c);
        solve(a);
    }
};

struct eda{
    int from,to,cost;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<eda> es(m);
    rep(i,m){
        int x,y,s;
        cin >> x >> y >> s;
        G[x].pb((edge){y,s});
        es[i] = (eda){x,y,s};
    }
    rrep(i,n){
        each(e,G[i]){
            cmx(d[i],d[e.to]+e.cost);
        }
    }
    int mx = d[0];
    vd b(m+1,0.0), c(n+m,0.0);
    rep(i,m){
        c[n+i] = 1.0;
    }
    vector<vd> A(m+1,vd(n+m,0.0));
    rep(i,m){
        A[i][es[i].from] = 1, A[i][es[i].to] = -1, A[i][n+i] = 1;
        b[i] = -es[i].cost;
    }
    A[m][n-1] = 1, b[m] = mx;
    Simplex sp(A,b,c);
    int ans = round(sp.ans);
    cout << ans << "\n";
    return 0;
}



Measured memory_bytes: 20284