import sys #input = sys.stdin.readline input = sys.stdin.buffer.readline from collections import defaultdict class SegmentTree(object): def __init__(self, A, dot, unit): n = 1 << (len(A) - 1).bit_length() tree = [unit] * (2 * n) for i, v in enumerate(A): tree[i + n] = v for i in range(n - 1, 0, -1): tree[i] = dot(tree[i << 1], tree[i << 1 | 1]) self._n = n self._tree = tree self._dot = dot self._unit = unit def __getitem__(self, i): return self._tree[i + self._n] def update(self, i, v): i += self._n self._tree[i] = v while i != 1: i >>= 1 self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1]) def add(self, i, v): self.update(i, self[i] + v) def sum(self, l, r): #これで[l,r)から取り出す。 l += self._n r += self._n l_val = r_val = self._unit while l < r: if l & 1: l_val = self._dot(l_val, self._tree[l]) l += 1 if r & 1: r -= 1 r_val = self._dot(self._tree[r], r_val) l >>= 1 r >>= 1 return self._dot(l_val, r_val) def main(): N,M,Q = map(int,input().split()) G = [[] for _ in range(Q)] #L = [] dic = defaultdict(list) for i in range(Q): a,b = map(int,input().split()) #a -= 1; b -= 1 #L.append((a,b)) #man,woman,index dic[a].append(b) #print(dic) KEY = list(dic.keys()) KEY.sort() Tree = SegmentTree([0]*(M+1),max,0) #womanがyの時の最大値 for x in KEY: L = sorted(dic[x]) #print(L) val = [] #if len(L) == 0: continue for y in L: temp = max(Tree.sum(0,y) + 1, Tree.__getitem__(y)) #print(val) val.append(temp) for i in range(len(val)): y = L[i] v = val[i] Tree.update(y,v) ans = 0 for i in range(M+1): ans = max(ans,Tree.__getitem__(i)) print(ans) if __name__ == '__main__': main()