num=[int(x) for x in input().split(" ")]
n=int(input())
jud=[] #判斷兩個商品都購買的條件是否成立
jud1=[] #判斷編號1購買是否成立
jud2=[] #判斷編號2購買是否成立
for i in range(n):
buy=[int(x) for x in input().split(" ")]
if num[0] in buy and num[1] in buy:
for i in range(len(buy)):
if buy[i]==num[0]:
jud1.append(1)
elif buy[i]==-1*num[0]:
jud1.append(-1)
elif buy[i]==num[1]:
jud2.append(1)
elif buy[i]==-1*num[1]:
jud2.append(-1)
if sum(jud1)>0 and sum(jud2)>0:
jud.append(1)
jud1.clear()
jud2.clear()
print(str(sum(jud)))