Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.

Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).

<image>

Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.

Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.

Input

The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.

The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.

Output

Print the minimal number of elements to be purchased.

Examples

Input

2 2 3
1 2
2 2
2 1


Output

0


Input

1 5 3
1 3
1 1
1 5


Output

2


Input

4 3 6
1 2
1 3
2 2
2 3
3 1
3 3


Output

1

Note

For each example you have a picture which illustrates it.

The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.

The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.

Test 1

We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.

<image>

Test 2

We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.

<image>

Test 3

There are several possible solutions. One of them is illustrated below.

Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.

<image>
20 20 20
18 16
4 20
2 5
7 4
11 13
6 10
20 8
14 6
3 12
5 1
16 7
10 9
1 11
12 18
19 15
13 19
17 3
9 17
15 2
8 14
 20 20 1
17 13
 1 1 0
 10000 9999 1
5717 9264
 20 10 5
18 10
19 10
19 9
20 9
20 8
 1 20 3
1 18
1 12
1 10
 20 10 20
9 5
15 6
17 10
14 1
18 7
7 4
2 3
19 6
6 6
16 10
5 2
3 5
12 6
10 6
11 1
4 1
20 5
13 8
1 9
8 7
 98 100 25
96 100
97 100
97 99
98 99
98 98
95 98
96 97
94 97
95 96
93 96
94 95
92 95
93 94
91 94
92 93
90 93
91 92
89 92
90 91
88 91
89 90
87 90
88 89
86 89
87 88
 5 5 5
2 4
3 3
5 1
4 1
2 1
 200000 200000 0
 2 2 2
1 1
2 2
 20 20 2
9 14
4 1
 2 2 3
1 2
2 1
2 2
 1 2 1
1 1
 2 2 4
1 1
1 2
2 1
2 2
 10000 10000 0
 1 200000 0
 20 20 80
5 3
13 13
8 5
2 9
12 16
1 11
15 11
3 20
10 7
5 4
11 2
5 20
14 8
5 1
8 13
11 5
19 2
15 12
12 7
16 5
17 3
12 2
17 16
12 3
12 6
18 20
2 20
9 1
5 10
9 18
17 1
17 10
20 1
12 12
19 14
7 8
2 19
6 14
5 6
15 2
18 14
5 7
14 14
17 2
20 20
11 6
18 15
10 5
20 3
1 8
18 8
6 3
9 7
14 20
15 1
7 14
13 17
3 18
18 9
14 13
6 10
19 13
11 11
17 8
3 5
9 12
12 17
19 1
19 15
11 12
5 9
1 9
3 13
5 14
9 15
18 11
20 12
4 20
3 9
8 2
 10 10 20
7 9
2 3
3 5
4 6
2 4
10 1
4 8
6 6
3 8
3 9
8 3
5 1
10 7
1 1
5 4
2 1
7 5
6 7
9 1
1 2
 20 20 39
3 16
4 8
2 11
3 8
14 13
10 1
20 10
4 13
13 15
11 18
14 6
9 17
5 4
18 15
18 9
20 20
7 5
5 17
13 7
15 16
6 12
7 18
8 6
16 12
16 14
19 2
12 3
15 10
17 19
19 4
6 11
1 5
12 14
9 9
1 19
10 7
11 20
2 1
17 3
 20 1 10
18 1
17 1
12 1
15 1
6 1
5 1
14 1
9 1
19 1
10 1
 20 20 39
13 7
12 3
16 1
11 1
11 4
10 14
9 20
5 12
5 18
14 17
6 3
17 13
19 14
2 14
6 4
15 13
15 5
5 10
16 16
9 7
15 8
9 15
3 7
1 14
18 1
12 7
14 2
7 16
8 14
9 5
6 19
7 14
4 14
14 11
14 9
9 6
14 12
14 13
20 14
 2 2 3
1 1
1 2
2 1
 200000 200000 1
113398 188829
 2 1 0
 100 100 0
 20 100 2
5 5
7 44
 20 20 39
18 20
19 20
19 19
20 19
20 18
17 18
18 17
16 17
17 16
15 16
16 15
14 15
15 14
13 14
14 13
12 13
13 12
11 12
12 11
10 11
11 10
9 10
10 9
8 9
9 8
7 8
8 7
6 7
7 6
5 6
6 5
4 5
5 4
3 4
4 3
2 3
3 2
1 2
2 1
 100 94 20
14 61
67 24
98 32
43 41
87 59
17 52
44 54
74 86
36 77
8 13
84 30
4 87
59 27
33 30
100 56
56 43
19 46
86 38
76 47
25 94
 200000 1 0
 2 2 3
1 1
2 1
2 2
 20 20 0
 240 100 25
238 100
239 100
239 99
240 99
240 98
237 98
238 97
236 97
237 96
235 96
236 95
234 95
235 94
233 94
234 93
232 93
233 92
231 92
232 91
230 91
231 90
229 90
230 89
228 89
229 88
 20 20 20
1 8
1 9
1 17
1 18
1 6
1 12
1 19
1 2
1 13
1 15
1 20
1 16
1 11
1 7
1 5
1 14
1 1
1 3
1 4
1 10
 20 20 20
17 19
13 18
5 11
19 1
17 16
1 19
3 16
17 10
13 19
5 10
2 7
18 17
16 20
8 8
8 13
4 4
1 17
17 18
17 7
16 11
 2 2 3
1 1
1 2
2 2
 2 2 1
1 2
 250 1 0
 20 20 30
6 15
2 6
16 14
13 7
6 8
13 17
12 3
7 13
5 20
10 10
2 20
1 12
12 11
15 4
7 18
10 12
4 19
18 19
4 1
9 13
17 2
11 5
4 9
20 8
3 1
14 14
8 4
19 4
11 2
16 16
 1 10000 0
 2 2 2
1 2
2 2
 2 2 2
1 2
2 1
 2 2 1
2 1
 10 20 0
 1 1 1
1 1
 250 250 0
 2 2 2
1 1
1 2
 20 1 0
 3 3 5
1 3
2 3
2 2
3 2
3 1
 1 2 0
 1 20 20
1 19
1 5
1 8
1 12
1 3
1 9
1 2
1 10
1 11
1 18
1 6
1 7
1 20
1 4
1 17
1 16
1 15
1 14
1 1
1 13
 20 20 37
16 11
14 20
10 1
14 4
20 19
20 15
5 15
19 20
13 19
11 19
18 18
4 13
12 12
1 12
6 8
18 6
7 9
3 16
4 7
9 11
7 1
12 5
18 16
20 14
9 16
15 15
19 3
6 15
18 10
14 9
2 11
18 2
8 11
17 9
4 5
20 17
19 7
 13 17 20
6 14
5 16
2 1
11 6
4 10
4 15
8 14
2 11
10 6
5 11
2 4
4 8
2 10
1 13
11 13
2 5
7 13
9 7
2 15
8 11
 20 1 20
13 1
10 1
5 1
17 1
12 1
18 1
1 1
9 1
6 1
14 1
20 1
11 1
2 1
3 1
8 1
16 1
4 1
7 1
15 1
19 1
 2 2 1
2 2
 100 20 1
13 9
 15 15 29
5 3
2 14
3 9
11 12
5 5
4 2
6 10
13 12
12 5
1 11
3 4
4 6
11 3
10 13
15 11
1 15
7 15
2 9
13 14
12 6
9 7
10 1
8 1
6 7
7 10
9 4
8 8
15 13
14 8
 2 2 2
1 1
2 1
 1 20 0
 250 250 1
217 197
 8 3 7
8 3
1 2
8 1
3 2
5 1
5 3
6 1
 2 2 0
 10000 1 0
 2 1 1
1 1
 20 20 20
6 5
13 8
9 20
5 15
10 2
12 12
15 4
7 18
18 10
17 13
11 11
20 7
16 19
8 6
3 3
2 16
4 1
1 17
19 14
14 9
 17 13 20
16 4
17 10
16 1
15 7
10 1
14 6
6 13
2 2
7 10
12 12
14 1
10 4
12 5
14 2
3 1
12 13
9 1
4 1
5 9
10 6
 2 20 0
 2 20 40
1 19
2 19
1 7
2 20
1 8
1 14
2 10
1 10
1 9
2 12
1 12
2 17
1 3
2 13
1 20
1 18
2 5
2 6
2 4
1 6
2 9
2 11
1 1
2 8
2 7
2 16
2 2
1 16
1 15
1 11
1 4
2 1
2 15
2 14
2 3
1 17
1 2
1 5
2 18
1 13
 1 250 0
 2 2 1
1 1
 2 20 10
1 7
2 9
2 16
1 4
1 8
1 19
1 20
1 9
2 5
2 6
 20 10 0
 20 20 20
10 13
12 13
14 13
20 13
18 13
3 13
19 13
2 13
13 13
5 13
9 13
6 13
16 13
1 13
17 13
11 13
15 13
7 13
4 13
8 13
 10 20 19
8 20
9 20
9 19
10 19
10 18
7 18
8 17
6 17
7 16
5 16
6 15
4 15
5 14
3 14
4 13
2 13
3 12
1 12
2 11
 2 2 2
2 1
2 2

19
 38
 1
 19997
 24
 17
 9
 172
 4
 399999
 1
 37
 0
 1
 0
 19999
 200000
 0
 1
 0
 10
 0
 0
 399998
 2
 199
 117
 0
 173
 200000
 0
 39
 314
 19
 20
 0
 2
 250
 9
 10000
 1
 1
 2
 29
 0
 499
 1
 20
 0
 2
 0
 2
 10
 0
 2
 118
 0
 1
 20
 498
 4
 3
 10000
 1
 19
 11
 21
 0
 250
 2
 11
 29
 19
 10
 1

#include <bits/stdc++.h>
using namespace std;
int n, m, q, x, y;
int ans;
int ansb;
int mostr, mostc;
int row, coloumn;
vector<int> a[200001];
vector<int> b[200001];
int aa[200001];
int bb[200001];
int se;
int sans, sansb;
set<pair<int, int> > s;
set<pair<int, int> > ss;
set<pair<int, int> > sz;
set<pair<int, int> > szsz;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  ans = n + m - 1;
  mostr = -1;
  mostc = -1;
  if (q == 0) {
    cout << n + m - 1;
    return 0;
  }
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;
    s.insert({x, y});
    ss.insert({x, y});
    sz.insert({x, y});
    szsz.insert({x, y});
    a[x].push_back(y);
    aa[x]++;
    b[y].push_back(x);
    bb[y]++;
    if (aa[x] > mostr) {
      mostr = aa[x];
      row = x;
    }
    if (bb[y] > mostc) {
      mostc = bb[y];
      coloumn = x;
    }
  }
  ans = 0;
  for (int i = 1; i <= m; i++) {
    se = s.size();
    s.insert({row, i});
    bool flag;
    flag = false;
    if (se != s.size()) {
      for (auto v : b[i]) {
        for (auto w : a[v]) {
          if (s.find({row, w}) != s.end() && w != i) {
            flag = true;
            break;
          }
        }
        if (flag) break;
      }
      if (flag == false) ans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i].size() == 0) ans++;
  }
  sans = ans;
  ans = 0;
  for (int i = m; i >= 1; i--) {
    se = ss.size();
    ss.insert({row, i});
    bool flag;
    flag = false;
    if (se != ss.size()) {
      for (auto v : b[i]) {
        for (auto w : a[v]) {
          if (ss.find({row, w}) != ss.end() && w != i) {
            flag = true;
            break;
          }
        }
        if (flag) break;
      }
      if (flag == false) ans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i].size() == 0) ans++;
  }
  ans = min(ans, sans);
  ansb = 0;
  for (int i = 1; i <= n; i++) {
    se = sz.size();
    sz.insert({i, coloumn});
    bool flag;
    flag = false;
    if (se != sz.size()) {
      for (auto v : a[i]) {
        for (auto w : b[v]) {
          if (sz.find({w, coloumn}) != sz.end() && w != i) {
            flag = true;
            break;
          }
        }
        if (flag) break;
      }
      if (flag == false) ansb++;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (b[i].size() == 0) ansb++;
  }
  sansb = ansb;
  ansb = 0;
  for (int i = n; i >= 1; i--) {
    se = szsz.size();
    szsz.insert({i, coloumn});
    bool flag;
    flag = false;
    if (se != szsz.size()) {
      for (auto v : a[i]) {
        for (auto w : b[v]) {
          if (szsz.find({w, coloumn}) != szsz.end() && w != i) {
            flag = true;
            break;
          }
        }
        if (flag) break;
      }
      if (flag == false) ansb++;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (b[i].size() == 0) ansb++;
  }
  cout << min(ans, ansb);
}
