#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> p, r;


void makeSet(int x){
  p[x] = x;
  r[x] = 0;
}

int findSet(int x){
  if(x != p[x])p[x] = findSet(p[x]);
  return p[x];
}

void link(int x, int y){
  if(r[x] > r[y]) p[y] = x;
  else {
    p[x] = y;
    if(r[x] == r[y]) r[y] = r[y] + 1;
  }
}



void unite(int x, int y){
  link(findSet(x), findSet(y));
}


struct Edge{
  int a, b, cost;

  bool operator<(const Edge& o) const{
    return cost < o.cost;
  }
};


struct Graph{
  int n;
  vector<Edge> es;

  int kruskal(){
    sort(es.begin(), es.end());

    int min_cost = 0;

    for(int i=0; i<es.size(); i++){
      Edge& e = es[i];
      if(!(findSet(e.a)==findSet(e.b))){
	min_cost += e.cost;
	unite(e.a, e.b);
      }
    }

    return min_cost;
  }
};

Edge e;


int main(){

  int V, E, i;
  Graph g;

  cin >> V >> E;

  p.resize(V);
  r.resize(V);

  for(i=0; i<V; i++) makeSet(i);

  for(i=0; i<E; i++){
    cin >> e.a >> e.b >>e.cost;
    g.es.push_back(e);
  }


  cout << g.kruskal() << endl;

  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

vector<int>E[200000];
int id[200000];
int col[200000];
bool b[200000];

bool dfs(int v,int k,int c){
	id[v]=k;
	col[v]=c;
	bool flag=true;
	for(int u:E[v]){
		if(id[u]==-1){
			flag&=dfs(u,k,!c);
		}
		else{
			if(col[u]==col[v])flag=false;
		}
	}
	return flag;
}
vector<int>v[200000];

int main(){
	int n,m;cin>>n>>m;
	rep(i,m){
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	memset(id,-1,sizeof(id));
	int cnt=0;
	rep(i,n){
		if(id[i]==-1){
			b[cnt]=dfs(i,cnt,0);
			cnt++;
		}
		v[id[i]].push_back(i);
	}
	ll c1=0,c2=0,c3=0;
	rep(i,cnt){
		if(v[i].size()==1){
			c1++;
			continue;
		}
		c2++;
		if(b[i])c3++;
	}
	cout<<c1*(n-c1)*2+c1*c1+c2*c2+c3*c3<<endl;
}