#include #include using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; int op(int x, int y) { return max(x, y); } int e() { return -1001001001; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, q; cin >> n >> m >> q; VI cnt1(n), cnt2(m); VI to1(n, -1), to2(m, -1); rep(_, q) { int a, b; cin >> a >> b; cnt1[a - 1]++; cnt2[b - 1]++; to1[a - 1] = b - 1; to2[b - 1] = a - 1; } vector valid1(n, true), valid2(m, true); rep(i, n) if (cnt1[i] >= 2 || cnt1[i] == 1 && cnt2[to1[i]] >= 2) valid1[i] = false; rep(i, m) if (cnt2[i] >= 2 || cnt2[i] == 1 && cnt1[to2[i]] >= 2) valid2[i] = false; VI idx1(n), idx2(m); { int p1 = 0; rep(i, n) if (valid1[i]) idx1[i] = p1++; int p2 = 0; rep(i, m) if (valid2[i]) idx2[i] = p2++; VI nto1(p1), nto2(p2); rep(i, n) if (valid1[i]) nto1[i] = cnt1[i] == 0 ? -1 : idx2[to1[i]]; rep(i, m) if (valid2[i]) nto2[i] = cnt2[i] == 0 ? -1 : idx1[to2[i]]; n = p1, m = p2, to1 = nto1, to2 = nto2; } int base = n, add = 0; segtree seg(n + m + 1); seg.set(base, 0); rep(i, n) { if (to1[i] != -1) { int j = to1[i]; int x = seg.prod(base, base + j + 1) + 1; seg.set(base + j + 1, max(x, seg.get(base + j + 1))); } else { base--, add++; } } int ans = seg.prod(base, base + m + 1) + add; cout << ans << endl; }