#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
using namespace std;
int main()
{
int m,n;
int sum[10000];
while(~scanf("%d%d",&n,&m))
{
memset(sum,0,sizeof(sum));
for(int i=1; i<=m; i++)
{
int a,b,c;
cin>>a>>b>>c;
if(a==b)
continue;
sum[a]+=c,sum[b]+=c;
}
sort(sum+1, sum+n+1);
cout<<sum[1]<<endl;
}
return 0;
}