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<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
using ll=long long;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=2e9;
const ll INF=1e18;
using P=pair<int,int>;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m,par[1005]={},ans=0,a,b;
int find(int x) {
  if(par[x]==x) {
    return x;
  }
  return par[x]=find(par[x]);
}
bool same(int x,int y) {
  return par[x]==par[y];
}
void unit(int x,int y) {
  x=find(x),y=find(y);
  if(x!=y) {
    par[x]=y;
  }
}
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for(int i=1; i<=1000; i++) {
    par[i]=i;
  }
  for(int i=0; i<m; i++) {
    cin >> a >> b;
    unit(a,b);
  }
  map<int,int> ma;
  for(int i=1; i<=n; i++) {
    ma[find(i)]++;
  }
  for(auto p:ma) {
    if(p.second==1) {
      ans++;
    }
    else {
      ans--;
    }
  }
  cout << abs(ans) << "\n";
}

C++: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<28;

struct union_find {
  vector<int> rank_;
  vector<int> size_;
  vector<int> rid_;

  union_find(int n) { rank_.resize(n); rid_.assign(n, -1); size_.resize(n, 1); }

  void unite(int u, int v) {
    u = root(u), v = root(v);
    if(u == v) { return; }
    size_[u] = size_[v] = size_[u] + size_[v];
    if(rank_[u] < rank_[v]) { rid_[u] = v; }
    else { rid_[v] = u; if(rank_[u] == rank_[v]) { rank_[u]++; } }
  }

  bool same(int u, int v) { return root(u) == root(v); }
  int root(int x) { if(rid_[x] < 0) return x; else return rid_[x] = root(rid_[x]); }
  int operator[](int x) { return root(x); }
  int size_of(int x) { return size_[x]; }
  
};

int main() {

  int N, M; cin >> N >> M;

  union_find uf(N);

  rep(i, M) {
    int s, t; cin >> s >> t; s--, t--;
    uf.unite(s, t);
  }

  int vils = 0;
  set<int> cities;

  rep(i, N) {
    if(uf.size_of(i) == 1) { vils ++; }
    else { cities.insert(uf[i]); }
  }

  cout << abs(vils - (int)cities.size()) << endl;

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


class UnionFind{

private:
    vector<int> rank,p;

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

  public:
      UnionFind(){};
      UnionFind(int size){
        rank.resize(size,0);
        p.resize(size,0);
        for(int i = 0; i < size; i++){
          makeSet(i);
        }
      }

    bool same(int x, int y){
      return  find(x) == find(y);
    }

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

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

private:
    void link(int x, int y){
      if(rank[x] > rank[y]){
        p[y] = x;
      }else{
        p[x] = y;
        if(rank[x] == rank[y]) rank[y]++;
      }
    }

};

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

    UnionFind uf(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        uf.unite(a,b);
    }
    vi a(n);
    rep(i,n)a[uf.find(i)]++;
    //rep(i,n)cout << " " << a[i];cout << endl;
    int s = 0, t = 0;
    rep(i,n){
        if(a[i] == 1)s++;
        if(a[i] > 1)t++;
    }
    cout << abs(s-t) << endl;

}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define Fi first
#define Se second
using namespace std;
static const LL INF = 1LL<<61LL;
typedef pair<int,int> PII;

int N,M;
int flg[1010];
PII V[1010];
int cnt1;
int cnt2;
int main(){
    cin>>N>>M;
    cnt2=N;
    rep(i,M){
        int a,b;
        cin>>a>>b;
        flg[a]=1;
        flg[b]=1;
        V[i].Fi=max(a,b);
        V[i].Se=min(a,b);
    }
    //sort(V,V+M);
    rep(i,M){
        bool was=false;
        rep(j,i){
            if(V[i].Fi==V[j].Fi||V[i].Se==V[j].Fi){ was=true; break; }
            if(V[i].Fi==V[j].Se||V[i].Se==V[j].Se){ was=true; break; }
        }
        if(!was)cnt1++;
    }
    //cout<<cnt1<<endl;
    FOR(i,1,N){
        if(flg[i]!=0)cnt2--;
    }
    cout<<abs(cnt1-cnt2)<<endl;
}