//Relive your past life.
//Face your demons.
//The past is never dead,it is not even past.
//The memories are not only the key to the past but...also to the future.
//coded in Rusty Lake
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define ll   long long
#define pb   push_back
#define mp   make_pair
#define orz  1000000007
#define fi   first
#define se   second
using namespace std;
int n,q,nxt[200005],f[200005];
priority_queue<pair<int,pair<int,int> > > p;
int get(int x){return x==f[x]?x:f[x]=get(f[x]);}
int main(){
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;++i)f[i]=i,nxt[i]=(i+1)%n;
    for(int i=1;i<=q;++i){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        p.push(mp(-c,mp(a,b)));
        p.push(mp(-c-1,mp(b,nxt[a])));
    }
    int ok=n-1;
    ll ans=0;
    while(ok){
        int c=p.top().fi,a=p.top().se.fi,b=p.top().se.se;
        p.pop();
        if(get(a)==get(b)) continue;
        --ok;
        ans+=-1ll*c;
        f[f[a]]=f[b];
        p.push(mp(c-2,mp(nxt[a],nxt[b])));
    }
    printf("%lld\n",ans);
//	system("pause");
	return 0;
}
