def rddata(n):
a = [0 for i in range(24)]
for i in range(n):
s,e = map(int,input().split())
for j in range(s,e+1):
a[j] = 1
return a;
x = int(input())
for t in range(x):
n,m = map(int,input().split())
a,b=rddata(n),rddata(m)
sum=0
for i in range(24):
if a[i] == 1 and b[i] == 1:
sum += 1
print(sum)