using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("4 2");
WillReturn.Add("2 3");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("2");
WillReturn.Add("111 111");
WillReturn.Add("111 111");
//1
}
else if (InputPattern == "Input3") {
WillReturn.Add("12");
WillReturn.Add("5 2");
WillReturn.Add("5 6");
WillReturn.Add("1 2");
WillReturn.Add("9 7");
WillReturn.Add("2 7");
WillReturn.Add("5 5");
WillReturn.Add("4 2");
WillReturn.Add("6 7");
WillReturn.Add("2 2");
WillReturn.Add("7 8");
WillReturn.Add("9 7");
WillReturn.Add("1 8");
//8
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct ABInfoDef
{
internal int A;
internal int B;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
var ABInfoList = new List<ABInfoDef>();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
ABInfoDef WillAdd;
WillAdd.A = wkArr[0];
WillAdd.B = wkArr[1];
ABInfoList.Add(WillAdd);
}
var InsUnionFind = new UnionFind();
var AppearSet = new HashSet<int>();
foreach (ABInfoDef EachABInfo in ABInfoList) {
int CurrA = EachABInfo.A;
int CurrB = EachABInfo.B;
if (AppearSet.Add(CurrA)) InsUnionFind.MakeSet(CurrA);
if (AppearSet.Add(CurrB)) InsUnionFind.MakeSet(CurrB);
InsUnionFind.Unite(CurrA, CurrB);
}
// 木ごとのノード数
var NodeSetDict = new Dictionary<int, HashSet<int>>();
// 木ごとの、辺の数のDict
var EdgeCntDict = new Dictionary<int, int>();
foreach (ABInfoDef EachABInfo in ABInfoList) {
int CurrA = EachABInfo.A;
int CurrB = EachABInfo.B;
int RootNode = InsUnionFind.FindSet(CurrA);
if (EdgeCntDict.ContainsKey(RootNode) == false) {
EdgeCntDict[RootNode] = 0;
}
EdgeCntDict[RootNode]++;
if (NodeSetDict.ContainsKey(RootNode) == false) {
NodeSetDict[RootNode] = new HashSet<int>();
}
NodeSetDict[RootNode].Add(CurrA);
NodeSetDict[RootNode].Add(CurrB);
}
long Answer = 0;
foreach (var EachPair in NodeSetDict) {
int NodeCnt = EachPair.Value.Count();
int EdgeCnt = EdgeCntDict[EachPair.Key];
Answer += Math.Min(NodeCnt, EdgeCnt);
}
Console.WriteLine(Answer);
}
}
// UnionFindクラス
internal class UnionFind
{
private class NodeInfoDef
{
internal int ParentNode;
internal int Rank;
}
private Dictionary<int, NodeInfoDef> mNodeInfoDict =
new Dictionary<int, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(int pNode)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(int pX, int pY)
{
int XNode = FindSet(pX);
int YNode = FindSet(pY);
int XRank = mNodeInfoDict[XNode].Rank;
int YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
}
}
// ノードを引数として、木の根を取得
internal int FindSet(int pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<int>();
int CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (int EachPathNode in PathNodeList) {
NodeInfoDef wkNodeInfo = mNodeInfoDict[EachPathNode];
wkNodeInfo.ParentNode = CurrNode;
mNodeInfoDict[EachPathNode] = wkNodeInfo;
}
return CurrNode;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}