// Slower C++ solution:
//It is made by M_sea

#include <algorithm>

#include <iostream>

#include <cstdlib>

#include <cstring>

#include <cstdio>

#include <cmath>

#define re register

typedef long long ll;

using namespace std;



inline void chkmin(int& x,int y) { if (y<x) x=y; }

inline int read() {

    int X=0,w=1; char c=getchar();

    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }

    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();

    return X*w;

}



const int N=400000+10;



int n,m,Q;

int f[N],fa[N];



inline int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }

inline void merge(int x,int y) {

    x=find(x),y=find(y);

    if (x!=y) fa[x]=y;

}



struct Edge { int u,v,w; } e[N];

bool operator <(Edge a,Edge b) { return a.w<b.w; }



int main() {

    n=read(),Q=read();

    memset(f,0x3f,sizeof(f));

    for (re int i=1;i<=Q;++i) {

        int a=read()+1,b=read()+1,c=read();

        chkmin(f[a%n+1],c+1),chkmin(f[b%n+1],c+2);

        e[++m]=(Edge){a,b,c};

    }

    int pos=1;

    for (re int i=2;i<=n;++i) if (f[i]<f[pos]) pos=i;

    for (re int i=pos%n+1,j=pos;i!=pos;j=i,i=i%n+1) chkmin(f[i],f[j]+2);

    for (re int i=1;i<n;++i) e[++m]=(Edge){i,i+1,f[i+1]};

    e[++m]=(Edge){n,1,f[1]};

    sort(e+1,e+m+1);

    for (re int i=1;i<=n;++i) fa[i]=i;

    int tot=0; ll ans=0;

    for (re int i=1;i<=m;++i) {

        int u=e[i].u,v=e[i].v,w=e[i].w;

        if (find(u)==find(v)) continue;

        ++tot,ans+=w,merge(u,v);

        if (tot==n-1) break;

    }

    printf("%lld\n",ans);

    return 0;

}

//qwqwq

// Faster C++ solution for the same problem (measured speedup: 1.4476029274373619x):
#include <cmath>

#include <cstdio>

#include <cstring>

#include <algorithm>

using std::min; using std::max;

using std::swap; using std::sort;

typedef long long ll;



template<typename T>

void read(T &x) {

	int flag = 1; x = 0; char ch = getchar();

	while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }

	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;

}



const int _ = 4e5 + 10;

int n, q, d[_], cnt, fa[_];

struct Edge { int u, v, w; } e[_];

inline bool operator < (const Edge &a, const Edge &b) { return a.w < b.w; }

inline void ChkMin(int &a, int b) { if(a > b) a = b; }

inline void addEdge(int u, int v, int w) { e[++cnt] = (Edge){u, v, w}; }

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

bool unionn(int x, int y) {

	int fx = find(x), fy = find(y);

	if(fx == fy) return 0;

	fa[fx] = fy; return 1;

}



int main () {

#ifdef OFFLINE_JUDGE

	freopen("233.in", "r", stdin);

	freopen("233.out", "w", stdout);

#endif

	read(n), read(q);

	memset(d, 127, sizeof d);

	for(int i = 1, a, b, c; i <= q; ++i) {

		read(a), read(b), read(c), addEdge(a, b, c);

		ChkMin(d[a], c + 1), ChkMin(d[b], c + 2);

	}

	for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i - 1 + n) % n] + 2);

	for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i - 1 + n) % n] + 2);

	for(int i = 0; i <= n; ++i) addEdge(i, (i + 1) % n, d[i]);

	sort(e + 1, e + cnt + 1);

	for(int i = 1; i < n; ++i) fa[i] = i;

	int blo = n; ll ans = 0;

	for(int i = 1; i <= cnt && blo > 1; ++i)

		if(unionn(e[i].u, e[i].v)) ans += e[i].w, --blo;

	printf("%lld\n", ans);

	return 0;

}