p03915 CODE FESTIVAL 2016 Final - Zigzag MST_9791
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:

<image>

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.

Examples

Input

7 1
5 2 1


Output

21


Input

2 1
0 0 1000000000


Output

1000000001


Input

5 3
0 1 10
0 2 10
0 4 10


Output

42
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

#define mod 1000000007
#define INF_LL 1145141919810364

#define MAX_N 200005
#define MAX_Q 200005

int N, Q;
LL A[MAX_Q], B[MAX_Q], C[MAX_Q];

void input()
{
  cin >> N >> Q;
  for (size_t i = 0; i < Q; i++) {
    scanf("%lld %lld %lld", &A[i], &B[i], &C[i]);
  }
}

class UnionFind{
public:
  UnionFind(int _n)
  {
    init(_n);
  }

  void init(int _n)
  {
    par.clear();
    par.resize(_n);
    for (size_t i = 0; i < par.size(); i++) {
      par[i] = i;
    }
  }

  int find(int _x)
  {
    if(_x == par[_x]){
      return _x;
    }
    else{
      return par[_x] = find(par[_x]);
    }
  }

  void unite(int _x, int _y)
  {
    _x = find(_x);
    _y = find(_y);
    if(_x == _y){
      return;
    }
    par[_x] = _y;
  }

  bool same(int _x, int _y)
  {
    return find(_x) == find(_y);
  }

private:
  vector<int> par;
};

LL dp[MAX_N][2];
LL Cost[MAX_N];

vector<pair<LL, pair<int, int>>> EdgeList;
LL solve()
{
  for (size_t i = 0; i < N; i++) {
    Cost[i] = INF_LL;
  }

  for (size_t i = 0; i < Q; i++) {
    Cost[A[i]] = min(Cost[A[i]], C[i]+1);
    Cost[B[i]] = min(Cost[B[i]], C[i]+2);
  }

  for (size_t d = 0; d < 2; d++) {
    for (size_t i = 0; i < N; i++) {
      Cost[(i+1)%N] = min(Cost[(i+1)%N], Cost[i]+2);
    }
  }

  UnionFind uf(N);
  for (size_t i = 0; i < Q; i++) {
    EdgeList.push_back(make_pair(C[i], make_pair(A[i], B[i])));
  }
  for (size_t i = 0; i < N; i++) {
    EdgeList.push_back(make_pair(Cost[i], make_pair(i, (i+1)%N)));
  }

  sort(EdgeList.begin(), EdgeList.end());

  LL ret = 0;
  for (size_t i = 0; i < EdgeList.size(); i++) {
    auto edge = EdgeList[i];
    if(!uf.same(edge.second.first, edge.second.second)){
      uf.unite(edge.second.first, edge.second.second);
      ret += edge.first;
    }
  }

  return ret;
}

int main()
{
  input();

  cout << solve() << endl;

  return 0;
}

7 1
5 2 1 2 1
0 0 1000000000 5 3
0 1 10
0 2 10
0 4 10 7 1
6 2 1 2 1
0 0 1000000010 5 3
0 0 10
0 2 10
0 4 10 9 1
6 2 1 5 3
0 0 10
0 3 10
0 4 10 9 1
7 2 1 9 1
5 2 1 7 1
5 4 1 2 1
0 0 0000000000 5 3
0 1 10
1 2 10
0 4 10 2 1
1 0 1000000010 9 1
6 2 0 2 1
0 0 0000000010 5 3
0 0 10
1 2 10
0 4 16 5 3
0 0 10
1 0 10
0 4 16 2 1
0 0 1000000100 5 3
0 1 10
0 2 7
0 4 10 2 1
0 0 1000001010 5 3
0 0 10
0 3 10
0 4 3 9 1
6 1 0 2 1
0 0 0000010010 9 1
1 2 0 12 1
3 2 1 2 1
0 0 1000010100 7 1
2 4 1 3 1
0 0 1000001010 2 1
0 0 0001010010 9 1
2 2 0 5 3
0 0 10
1 0 10
0 4 0 12 1
3 4 1 2 1
0 0 1000010110 3 1
1 0 1000001010 13 1
6 1 -1 5 3
0 0 10
0 2 10
1 4 2 12 1
3 4 2 2 1
0 0 1000010010 12 1
6 1 -1 21 1
3 4 2 12 1
2 1 -1 21 1
3 4 0 5 1
2 1 -1 21 1
3 4 1 5 1
1 1 -1 21 1
1 4 1 5 1
0 1 -1 21 1
0 4 1 9 1
0 1 -1 9 1
0 2 -1 6 1
5 2 1 2 1
0 0 1000100000 7 1
6 2 0 2 1
0 0 1000100010 9 1
6 4 1 5 3
0 1 10
1 2 10
0 4 9 4 1
1 0 1000000010 2 1
0 1 0000000010 12 1
5 2 0 7 1
2 0 1 2 1
0 0 1000001000 16 1
6 1 0 2 1
0 0 0000010011 4 1
0 0 1000001010 9 1
6 2 -1 12 1
6 4 1 4 1
1 0 1000001010 5 3
1 0 2
1 2 20
0 4 10 13 1
6 1 0 7 1
3 2 0 12 1
1 4 2 21 1
6 4 2 21 1
0 4 0 7 1
1 1 -1 21 1
1 0 1 2 1
1 1 -1 32 1
0 4 1 2 1
0 0 1000110010 9 1
6 4 2 3 1
1 0 1000000010 2 1
1 0 1000001000 5 3
0 0 10
0 0 10
1 4 3 8 3
0 0 10
1 2 10
0 4 10 5 3
1 0 14
1 2 10
0 4 21 12 1
0 2 0 9 1
6 6 1 5 3
1 0 1
1 2 20
0 4 10 13 1
6 1 1 12 1
1 7 2 21 1
6 4 1 21 1
1 4 0 32 1
0 5 1 3 1
2 0 1000000010 14 3
0 0 10
1 2 10
0 4 10 9 1
2 0 0 13 1
6 0 1 12 1
2 7 2 28 1
6 4 1 34 1
1 4 0 29 1
0 0 1 4 1
2 0 1000000010 2 1
1 1 0000011011
21 1000000001 42 22
 1000000011
 43
 36
 44
 37
 39
 31
 1
 42
 1000000010
 28
 11
 47
 49
 1000000101
 34
 1000001011
 21
 29
 10011
 56
 111
 1000010101
 27
 2000002024
 1010011
 64
 9
 121
 1000010111
 2000002021
 57
 14
 132
 1000010011
 45
 420
 89
 380
 5
 400
 12
 330
 8
 301
 48
 35
 15
 1000100001
 16
 1000100011
 46
 41
 3000000034
 10
 70
 24
 1000001001
 120
 10012
 3000003039
 20
 94
 3000003034
 17
 69
 25
 98
 366
 281
 30
 381
 0
 796
 1000110011
 54
 2000000021
 1000001000
 18
 85
 50
 91
 72
 13
 81
 77
 346
 310
 749
 2000000022
 223
 38
 78
 80
 654
 934
 812
 3000000033
 11012
