PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000050. Bảng hóa học (Chemical Table)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bảng hóa học (Chemical Table)

Chemical Table

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A laboratory has a table with nn rows and mm columns. Some cells already contain samples; the remaining cells are empty. The initially occupied cells are specified by their row and column coordinates.

The laboratory can create a new sample for free as follows: choose an axis-aligned rectangle whose four corners are cells of the table. If samples already occupy three corners, a sample can be placed at the fourth corner. Existing samples remain, and newly created samples may be used in later operations.

The laboratory can also buy a sample and place it in any empty cell, at a unit cost. Find the minimum number of samples that must be bought so that eventually every cell contains a sample.

Input

The first line contains n,m,qn,m,q. Each of the next qq lines has row rir_i and column cic_i of an initially known cell.

Output

Print the minimum number of purchases.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤201\le n,m\le20, 0≤q≤250\le q\le25; q≤nmq\le nm; 1≤ri≤n1\le r_i\le n, 1≤ci≤m1\le c_i\le m; initially occupied cells are distinct.
  • Subtask 2 (30 points): 1≤n,m≤42001\le n,m\le4200, 0≤q≤44600\le q\le4460; q≤nmq\le nm; 1≤ri≤n1\le r_i\le n, 1≤ci≤m1\le c_i\le m; initially occupied cells are distinct.
  • Subtask 3 (50 points): 1≤n,m≤2⋅1051\le n,m\le2\cdot10^5, 0≤q≤2⋅1050\le q\le2\cdot10^5; q≤nmq\le nm; 1≤ri≤n1\le r_i\le n, 1≤ci≤m1\le c_i\le m; initially occupied cells are distinct.

Examples

Input

2 2 3
1 2
2 2
2 1

Output

0

Explanation

The three known cells are three corners of a 2-by-2 rectangle; the missing corner (1,1) can be created without any purchase, so the answer is 0.