Problem:
Score : 1300 points

Problem Statement

We have a graph with N vertices, numbered 0 through N-1. Edges are yet to be added.

We will process Q queries to add edges.
In the i-th (1≦i≦Q) query, three integers A_i, B_i and C_i will be given, and we will add infinitely many edges to the graph as follows:

The two vertices numbered A_i and B_i will be connected by an edge with a weight of C_i.

The two vertices numbered B_i and A_i+1 will be connected by an edge with a weight of C_i+1.

The two vertices numbered A_i+1 and B_i+1 will be connected by an edge with a weight of C_i+2.

The two vertices numbered B_i+1 and A_i+2 will be connected by an edge with a weight of C_i+3.

The two vertices numbered A_i+2 and B_i+2 will be connected by an edge with a weight of C_i+4.

The two vertices numbered B_i+2 and A_i+3 will be connected by an edge with a weight of C_i+5.

The two vertices numbered A_i+3 and B_i+3 will be connected by an edge with a weight of C_i+6.

...

Here, consider the indices of the vertices modulo N.
For example, the vertice numbered N is the one numbered 0, and the vertice numbered 2N-1 is the one numbered N-1.

The figure below shows the first seven edges added when N=16, A_i=7, B_i=14, C_i=1:

After processing all the queries, find the total weight of the edges contained in a minimum spanning tree of the graph.

Constraints

2≦N≦200,000

1≦Q≦200,000

0≦A_i,B_i≦N-1

1≦C_i≦10^9

Input

The input is given from Standard Input in the following format:

N Q
A_1 B_1 C_1
A_2 B_2 C_2
:
A_Q B_Q C_Q

Output

Print the total weight of the edges contained in a minimum spanning tree of the graph.

Sample Input 1

7 1
5 2 1

Sample Output 1

21

The figure below shows the minimum spanning tree of the graph:

Note that there can be multiple edges connecting the same pair of vertices.

Sample Input 2

2 1
0 0 1000000000

Sample Output 2

1000000001

Also note that there can be self-loops.

Sample Input 3

5 3
0 1 10
0 2 10
0 4 10

Sample Output 3

42

Solution in C++ (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
namespace IO
{
    char ibuf[(1<<21)+1],obuf[(1<<21)+1],st[15],*iS,*iT,*oS=obuf,*oT=obuf+(1<<21);
	inline char Get()
	{
        return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++);
    }
    inline void Flush()
	{
        fwrite(obuf,1,oS-obuf,stdout);
        oS=obuf;
    }
    inline void Put(char x)
	{
        *oS++=x;
        if(oS==oT)
			Flush();
    }
   	inline int read()
	{
	   	register int x=0;
		register char ch=Get();
    	while(ch>'9'||ch<'0')
    		ch=Get();
    	while(ch>='0'&&ch<='9')
       		x=(x<<3)+(x<<1)+(ch^48),ch=Get();
       	return x;
	}
    inline void write(int x)  
	{  
		register int top=0;
    	while(x) 
			st[++top]=(x%10)+48,x/=10;
   		while(top)
			Put(st[top--]);
	}
}
using namespace IO;
const int N=200001,M=N<<3;
struct Edge
{
    int u,v,w,Next;
    Edge(){}
    Edge(int U,int V,int W,int NEXT)
    {
        u=U,v=V,w=W,Next=NEXT;
    }
    bool operator<(Edge a)const
    {
            return w<a.w;
    }
}edge[M];
int head[N],tot,f[N],d[N];
inline void add(int x,int y,int z)
{
    edge[++tot]=Edge(x,y,z,head[x]),head[x]=tot;
}
int find(int x)
{
    return x==f[x]? x:f[x]=find(f[x]);
}
bool cmp(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx==fy)
        return false;
    if(fy>fx)
        f[fy]=fx;
    else
        f[fx]=fy;
    return true;
}
int main()
{
    register int n=read(),q=read();
    memset(d,0x3f,sizeof d);
    for(register int i=1,a,b,c;i<=q;++i)
    {
        a=read(),b=read(),c=read();
        add(a,b,c);
        d[a]=min(d[a],c+1),d[b]=min(d[b],c+2);
    }
    for(register int i=0;i<n;++i)
        d[i]=min(d[i],d[(i-1+n)%n]+2);
    for(register int i=0;i<n;++i)
        d[i]=min(d[i],d[(i-1+n)%n]+2);
    for(register int i=0;i<=n;++i)
        add(i,(i+1)%n,d[i]);
    sort(edge+1,edge+1+tot);
    for(register int i=1;i<n;++i)
		f[i]=i;
    register int lt=n;
	long long ans=0;
    for(register int i=1;i<=tot&&lt>1;++i)
        if(cmp(edge[i].u, edge[i].v))
            ans+=edge[i].w,--lt;
    return printf("%lld",ans),0;
}