We have a grid with N rows and N columns. The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white. Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

* If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.



Find the number of black cells when no more white cells can be painted black.

Constraints

* 1≤N≤10^5
* 1≤M≤10^5
* 1≤a_i,b_i≤N
* All pairs (a_i, b_i) are distinct.

Input

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


N M
a_1 b_1
a_2 b_2
:
a_M b_M


Output

Print the number of black cells when no more white cells can be painted black.

Examples

Input

3 2
1 2
2 3


Output

3


Input

2 2
1 1
1 2


Output

4


Input

4 3
1 2
1 3
4 4


Output

3
#include <bits/stdc++.h>
using namespace std;



int main(){
    int N, M;
    cin >> N >> M;
    vector<pair<int, int>> edges[100000];
    for(int i=0; i<M; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        edges[a].emplace_back(b, 1);
        edges[b].emplace_back(a, 2);
    }

    vector<int> col(N, -1);
    int64_t ans = 0;
    for(int s=0; s<N; s++) if(col[s] == -1){
        queue<int> que;
        que.push(s);
        col[s] = 0;
        vector<int64_t> num(3);
        bool cont = false;
        int es = 0;
        while(que.size()){
            int i = que.front(); que.pop();
            num[col[i]]++;
            for(auto& e : edges[i]){
                int j = e.first, c = (col[i] + e.second) % 3;
                if(e.second == 1) es++;
                if(col[j] == -1){
                    col[j] = c;
                    que.push(j);
                }else if(col[j] != c){
                    cont = true;
                }
            }
        }
        if(cont){
            int64_t s = num[0] + num[1] + num[2];
            ans += s*s;
        }else if(num[0] && num[1] && num[2]){
            ans += num[0]*num[1] + num[1]*num[2] + num[2]*num[0];
        }else{
            ans += es;
        }
    }
    cout << ans << endl;
    return 0;
}
