code:
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
namespace Set{
int f[N],r[N];
void init(int n,int state){
int m = n * state;
for(int i = 1;i <= m;++ i){
f[i] = i;
r[i] = 1;
}
}
int find(int w){
if(f[w] == w) return w;
return f[w] = find(f[w]);
}
void merge(int x,int y){
x = find(x),y = find(y);
if(x == y) return;
if(r[x] > r[y]) swap(x,y);
r[y] += r[x];
f[x] = y;
}
void build(int x,int y,int n,int g){
if(g){
merge(x + n,y);
merge(x,y + n);
}
else merge(x,y);
}
int count(int n){
set<int> s;
for(int i = 1;i <= n;++ i) s.insert(f[i]);
return s.size();
}
}
int main(){
int n,T;
cin>>n>>T;
Set::init(n,2);
while(T --){
char op;
int x,y;
cin>>op>>x>>y;
if(op == 'F') Set::build(x,y,n,0);
else Set::build(x,y,n,1);
}
cout<<Set::count(n);
return 0;
}