function spilitInput(input, to_int) {
  if (typeof to_int === 'undefined') to_int = false;
  var splited = [];
  var rows = input.split('\n');
  for (var i = 0; i < rows.length; i += 1) {
    if (to_int) {
      splited.push(rows[i].split(' ').map(function(d){return parseInt(d,10);}));
    } else {
      splited.push(rows[i].split(' '));
    }
  }
  return splited;
}

function initDp(N, init_n) { return Array(N+2).fill(init_n); }

function Main(input) {
  var splited = spilitInput(input, true);
  var n = splited[0][0];
  var m = splited[0][1];
  
  var dp = initDp(n, 0); // dpの初期化（要素数, 各要素の初期値）
  
  var route_groups = [];
  route_groups.push([]);
  route_groups[0].push(splited[1][0]);
  route_groups[0].push(splited[1][1]);
    dp[splited[1][0]] += 1;
    dp[splited[1][1]] += 1;
  
  var substruct_count = 0;
  for(var i = 2; i < splited.length; i += 1) {
    var matched_group_count = 0;
    for(var j = 1; j < route_groups.length; j += 1) {
      if (route_groups[j].indexOf(splited[i][0]) !== -1 || route_groups[j].indexOf(splited[i][1]) !== -1) {
        route_groups[j].push(splited[i][0]);
        route_groups[j].push(splited[i][1]);
        matched_group_count += 1;
      }
    }
    if (matched_group_count >= 2) {
      substruct_count += (matched_group_count - 1);
    }
    else if(matched_group_count == 0) {
      route_groups.push([]);
      route_groups[(route_groups.length - 1)].push(splited[1][0]);
      route_groups[(route_groups.length - 1)].push(splited[1][1]);
    }
    dp[splited[i][0]] += 1;
    dp[splited[i][1]] += 1;
  }
  
  var zero_count = (dp.filter(function(d) { return d == 0; }).length - 2);
  
  console.log((route_groups.length - (substruct_count + 1) + zero_count));
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));