
#include<bits/stdc++.h>
using namespace std;

#define int long long

//#define X first
#define Y second
#define eb emplace_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP rep
using vint=vector<int>;
typedef long long ll;
typedef ll LL;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> inline bool MX(T&l, const T&r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T&l, const T&r){return l>r?l=r,1:0;}

int N;
int M;
char cell[1145][1145];
bool used[114514];
vint es[114514];

//#define int long long

const long long INF=(1ll<<50);
struct graph{
  typedef long long flow_type;
  struct edge{
    int src,dst;
    flow_type capacity,flow;
    size_t rev;
  };
  int n;
  vector<vector<edge>> adj;
  graph(int n) : n(n),adj(n){}
  void add_edge(int src,int dst,flow_type capacity){
    adj[src].push_back({src,dst,capacity,0,adj[dst].size()});
    adj[dst].push_back({dst,src,0,0,adj[src].size()-1});
  }
  flow_type max_flow(int s,int t){
    vector<int> level(n),iter(n);
    function<int(void)> levelize = [&](){
      level.assign(n,-1); level[s]=0;
      queue<int> Q;Q.push(s);
      while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(u==t) break;
        for(auto &e: adj[u]){
          if(e.capacity > e.flow && level[e.dst] <0){
            Q.push(e.dst);
            level[e.dst] = level[u]+1;
          }
        }
      }
      return level[t];
    };
    function<flow_type(int,flow_type)> augment = [&](int u,flow_type cur){
      if(u==t) return cur;
      for(int &i=iter[u];i<adj[u].size();++i){
        edge &e = adj[u][i],&r=adj[e.dst][e.rev];
        if(e.capacity>e.flow && level[u]<level[e.dst]){
          flow_type f=augment(e.dst,min(cur,e.capacity -e.flow));
          if(f>0){
            e.flow+=f;
            r.flow-=f;
            return f;
          }
        }
      }
      return flow_type(0);
    };
    for(int u=0;u<n;++u)
      for(auto &e:adj[u]) e.flow=0;

    flow_type flow=0;
    while(levelize()>=0){
      fill(all(iter),0);
      for(flow_type f;(f=augment(s,INF))>0;)
        flow+= f;
    }
    return flow;
  }
};

void dfs(int v) {
  used[v] = true;
  for (int u : es[v]) {
    if (used[u]) continue;
    dfs(u);
  }
}

signed  main(){
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  cin >> N >> M;
  graph G(N+N+2);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    es[x].eb(y+N);
    es[y+N].eb(x);
    G.add_edge(x, y+N, 1);
  }

  rep(i, N) {
    G.add_edge(N+N, i, 1);
    G.add_edge(i+N, N+N+1, 1);
  }

  int num = 0;
  rep(i, N+N) {
    if (used[i]) continue;
    if (es[i].empty()) continue;
    num++;
    dfs(i);
  }

  cout << M-G.max_flow(N+N, N+N+1) << " " << M-num << endl;
} 


### Prompt

Please provide a CPP coded solution to the problem described below:

Problem Statement

"Rooks Game" is a single-player game and uses a chessboard which has $N \times N$ grid and $M$ rook pieces.

A rook moves through any number of unoccupied squares horizontally or vertically. When a rook can attack another rook, it can capture the rook and move to the square which was occupied. Note that, in Rooks Game, we don't distinguish between white and black, in other words, every rook can capture any of other rooks.

<image>

Initially, there are $M$ rooks on the board. In each move, a rook captures another rook. The player repeats captures until any rook cannot be captured. There are two types of goal of this game. One is to minimize the number of captured rooks, and the other is to maximize it.

In this problem, you are requested to calculate the minimum and maximum values of the number of captured rooks.

* * *

Input

The input consists of a single test case in the format below.

> $N$ $M$ $x_{1}$ $y_{1}$ $\vdots$ $x_{M}$ $y_{M}$

The first line contains two integers $N$ and $M$ which are the size of the chessboard and the number of rooks, respectively ($1 \le N, M \le 1000$). Each of the following $M$ lines gives the position of each rook. The $i$-th line with $x_{i}$ and $y_{i}$ means that the $i$-th rook is in the $x_{i}$-th column and $y_{i}$-th row ($1 \le x_{i}, y_{i} \le N$). You can assume any two rooks are not in the same place.

Output

Output the minimum and maximum values of the number of captured rooks separated by a single space.

Examples

Input| Output
---|---


8 3
1 1
1 8
8 8


|


1 2



8 4
1 1
1 8
8 8
8 1


|


2 3



5 5
1 1
2 2
3 3
4 4
5 5


|


0 0



100 1
100 100


|


0 0



10 12
1 2
1 4
1 6
3 6
5 6
10 7
8 7
6 7
4 7
2 7
7 3
9 5


|


7 8






Example

Input




Output

### Response

```cpp

#include<bits/stdc++.h>
using namespace std;

#define int long long

//#define X first
#define Y second
#define eb emplace_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP rep
using vint=vector<int>;
typedef long long ll;
typedef ll LL;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> inline bool MX(T&l, const T&r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T&l, const T&r){return l>r?l=r,1:0;}

int N;
int M;
char cell[1145][1145];
bool used[114514];
vint es[114514];

//#define int long long

const long long INF=(1ll<<50);
struct graph{
  typedef long long flow_type;
  struct edge{
    int src,dst;
    flow_type capacity,flow;
    size_t rev;
  };
  int n;
  vector<vector<edge>> adj;
  graph(int n) : n(n),adj(n){}
  void add_edge(int src,int dst,flow_type capacity){
    adj[src].push_back({src,dst,capacity,0,adj[dst].size()});
    adj[dst].push_back({dst,src,0,0,adj[src].size()-1});
  }
  flow_type max_flow(int s,int t){
    vector<int> level(n),iter(n);
    function<int(void)> levelize = [&](){
      level.assign(n,-1); level[s]=0;
      queue<int> Q;Q.push(s);
      while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(u==t) break;
        for(auto &e: adj[u]){
          if(e.capacity > e.flow && level[e.dst] <0){
            Q.push(e.dst);
            level[e.dst] = level[u]+1;
          }
        }
      }
      return level[t];
    };
    function<flow_type(int,flow_type)> augment = [&](int u,flow_type cur){
      if(u==t) return cur;
      for(int &i=iter[u];i<adj[u].size();++i){
        edge &e = adj[u][i],&r=adj[e.dst][e.rev];
        if(e.capacity>e.flow && level[u]<level[e.dst]){
          flow_type f=augment(e.dst,min(cur,e.capacity -e.flow));
          if(f>0){
            e.flow+=f;
            r.flow-=f;
            return f;
          }
        }
      }
      return flow_type(0);
    };
    for(int u=0;u<n;++u)
      for(auto &e:adj[u]) e.flow=0;

    flow_type flow=0;
    while(levelize()>=0){
      fill(all(iter),0);
      for(flow_type f;(f=augment(s,INF))>0;)
        flow+= f;
    }
    return flow;
  }
};

void dfs(int v) {
  used[v] = true;
  for (int u : es[v]) {
    if (used[u]) continue;
    dfs(u);
  }
}

signed  main(){
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  cin >> N >> M;
  graph G(N+N+2);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    es[x].eb(y+N);
    es[y+N].eb(x);
    G.add_edge(x, y+N, 1);
  }

  rep(i, N) {
    G.add_edge(N+N, i, 1);
    G.add_edge(i+N, N+N+1, 1);
  }

  int num = 0;
  rep(i, N+N) {
    if (used[i]) continue;
    if (es[i].empty()) continue;
    num++;
    dfs(i);
  }

  cout << M-G.max_flow(N+N, N+N+1) << " " << M-num << endl;
} 


```