Go: package main

import (
    "fmt"
    "bufio"
    "strconv"
    "os"
    "strings"
    "math"
)

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
    buf := make([]byte, 0, 1000000)
    for {
        l, p, e := rdr.ReadLine()
        if e != nil {
            panic(e)
        }
        buf = append(buf, l...)
        if !p {
            break
        }
    }
    return string(buf)
}

func toInt(str string) int  {
    n, _ := strconv.Atoi(str)
    return n
}

func abs(a int) int {
    return int(math.Abs(float64(a)))
}

func getPar(x int, parent[1000]int) int {
    if parent[x] == x {
        return x
    } else {
        parent[x] = getPar(parent[x], parent)
        return parent[x]
    }
}

func main() {
    nm := strings.Split(readLine(), " ")
    n := toInt(nm[0])
    m := toInt(nm[1])
    var parent [1000]int
    for i := 0;i < n; i++ {
        parent[i] = i
    }


    for i := 0; i < m; i++ {
        ab := strings.Split(readLine(), " ")
        pa := getPar(toInt(ab[0]) - 1, parent)
        pb := getPar(toInt(ab[1]) - 1, parent)
        parent[pb] = pa
    }

    cities := make(map[int]int)
    villageCnt := 0
    for i := 0;i < n; i ++ {
        pi := getPar(i, parent)
        if i != pi {
            cities[pi] = 1
        } else {
            villageCnt ++
        }
    }
    cityCnt := len(cities)
    fmt.Println(abs(cityCnt * 2 - villageCnt))
}

C++: #include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()

struct UnionFind{
    int n;
    vector<int> p;
    UnionFind(int nn) : n(nn+1){
        p.resize(n);
        rep(i, n) p[i] = i;
    }
    int root(int x){
        if(p[x] == x) return x;
        else return p[x] = root(p[x]);
    }
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x != y) p[y] = x;
    }
    bool query(int x, int y){
        return root(x) == root(y);
    }
};


int main(){
    ios::sync_with_stdio(false);
    int N, M; cin >> N >> M;
    UnionFind ut(N);
    set<int> c;
    rep(i, M){
        int a, b; cin >> a >> b;
        a--; b--;
        if(a != b){
            c.insert(a);
            c.insert(b);
        }
        ut.unite(a, b);
    }
    set<int> s;
    rep(i, N){
        int r = ut.root(i);
        if(r != i) s.insert(ut.root(i));
    }
    int city = s.size();
    int vil = N - c.size();
    cout << abs(vil - city) << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int group[1010];
int parent(int i){
	if(i==group[i])	return i;
	else return group[i]=parent(group[i]);
}
void merge(int i,int j){
	i=parent(i);	j=parent(j);
	group[i]=group[j];
}

int main(){
	int n;	cin>>n;
	int m;	cin>>m;
	for(int i=1;i<=n;i++)	group[i]=i;
	for(int j=0;j<m;j++){
		int a,b;	cin>>a>>b;
		merge(a,b);
	}
	map<int,int> kind;
	for(int i=1;i<=n;i++)	kind[parent(i)]++;
	int village=0,city=0;
	for(auto it:kind){
		if(it.second==1)	village++;
		else 	city++;
	}
	cout<<abs(village-city)<<endl;
	return 0;
}

C++: #include <fstream>
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>

using namespace std;
#define ALL(c) (c).begin(), (c).end()
#define REP(i,n) for(ll i=0; i < (n); ++i)
using ll = long long;
using vl = vector<ll>;

struct union_find{
	vl v;
	union_find(ll n) : v(n, -1){}
	ll find(ll x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
	void unite(ll x, ll y){
		x = find(x); y = find(y);
		if (x == y) return;
		if (-v[x] < -v[y]) swap(x, y);
		v[x] += v[y];
		v[y] = x;
	}
	bool same(ll x, ll y){
		return find(x) == find(y);
	}
	ll size(ll x) { return -v[find(x)]; }
};

int main(){
#ifdef _WIN32
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cout << fixed << setprecision(8);

	ll n, m; cin >> n >> m;
	vl a(n), b(n);
	REP(i, m) cin >> a[i] >> b[i];

	union_find uf(n);
	REP(i, m) uf.unite(a[i]-1, b[i]-1);
	
	ll cntm = 0;
	ll cnts = 0;
	REP(i, n){
		if (uf.size(i) == 1){
			cntm++;
		}
		else{
			if (uf.find(i) == i){
				cnts++;
			}
		}
	}

	cout << abs(cntm - cnts) << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int dat[125252];
void init(){
  REP(i,125252)dat[i]=-1;
}
int root(int x){
  return dat[x]<0 ? x : (dat[x]=root(dat[x]));
}
void unite(int x,int y){
  x=root(x);
  y=root(y);
  if(x!=y){
    if(dat[x]>dat[y])swap(x,y);
    dat[x] += dat[y];
    dat[y] = x;
  }
}
bool same(int x,int y){
  return root(x)==root(y);
}
int size(int x){
  return -dat[x];
}

int main(){
  int n,m;
  cin>>n>>m;

  init();
  while(m--){
    int a,b;
    cin>>a>>b;
    --a; --b;
    unite(a,b);
  }
  int res = 0;
  REP(i,n){
    if(i!=root(i))continue;
    if(size(i)==1)--res;
    else ++res;
  }
  cout<<abs(res)<<endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pro 10000
using namespace std;

int parent[pro+1];

int find(int n){
	if(n==parent[n])return n;
	else find(parent[n]);
}

int main(){
	int n,m,city=0,town=0;
	cin>>n>>m;
	bool check[n+1];
	int a,b;
	loop(i,1,n+1)parent[i]=i;
	rep(i,m){
		cin>>a>>b;
		int top=find(a);
		parent[b]=top;
	}
	vector<int> vec[pro+1];
	loop(i,1,n+1){
		vec[parent[i]].push_back(i);
	}
	loop(i,1,n+1){
		if(vec[i].size()==0)continue;
		if(vec[i].size()==1)town++;
		else city++;
	}
	cout<<abs(city-town)<<endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pro 10000
using namespace std;

int parent[pro+1];

int find(int n){
	if(n==parent[n])return n;
	else find(parent[n]);
}

int abs(int n){
	if(n>0)return n;
	else return -1*n;
}

int main(){
	int n,m,city=0,town=0;
	cin>>n>>m;
	int a,b;
	loop(i,1,n+1)parent[i]=i;
	rep(i,m){
		cin>>a>>b;
		parent[b]=find(a);
	}
	vector<int> vec[pro+1];
	loop(i,1,n+1){
		vec[find(parent[i])].push_back(i);
	}
	loop(i,1,n+1){
		if(vec[i].size()==1)town++;
		if(vec[i].size()>1) city++;
	}
	cout<<abs(city-town)<<endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pro 10000
using namespace std;

int parent[pro+1];

int find(int n){
	if(n==parent[n])return n;
	else find(parent[n]);
}

int abs(int n){
	if(n>0)return n;
	else return -1*n;
}

int main(){
	int n,m,city=0,town=0;
	cin>>n>>m;
	int a,b;
	rep(i,n)parent[i]=i;
	rep(i,m){
		cin>>a>>b;
		a--;
		b--;
		parent[b]=parent[a]=find(a);
	}
	vector<int> vec[n];
	rep(i,n){
		vec[find(i)].push_back(i);
	}
	rep(i,n){
		if(vec[i].size()==1)town++;
		if(vec[i].size()>1) city++;
	}
	cout<<abs(city-town)<<endl;
	return 0;
}