package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func binarySearch(needle int, haystack []int) bool {
low := 0
high := len(haystack) - 1
for low <= high {
median := (low + high) / 2
if haystack[median] < needle {
low = median + 1
} else {
high = median - 1
}
}
if low == len(haystack) || haystack[low] != needle {
return false
}
return true
}
func input() []int {
var input string
var output []int
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input = scanner.Text()
inputSplit := strings.Split(input, " ")
for i := 0; i < len(inputSplit); i++ {
charInt, _ := strconv.Atoi(inputSplit[i])
output = append(output, charInt)
}
return output
}
func main() {
var aBreaks, yBreaks []int
var boretimeNow, output int
boretime := input()[0]
aLenght := input()[0]
for i := 0; i < aLenght; i++ {
ln := input()
aBreaks = append(aBreaks, ln[0])
aBreaks = append(aBreaks, ln[1])
}
yLenght := input()[0]
for i := 0; i < yLenght; i++ {
ln := input()
yBreaks = append(yBreaks, ln[0])
yBreaks = append(yBreaks, ln[1])
}
var max int
for i := 1; i < len(yBreaks); i++ {
if max < yBreaks[i] {
max = yBreaks[i]
}
}
for i := 1; i < len(aBreaks); i++ {
if max < aBreaks[i] {
max = aBreaks[i]
}
}
var u, q int
var aBreaksList []int
for true {
if u == len(aBreaks) {
break
}
if aBreaks[u]+q != aBreaks[u+1]+1 {
aBreaksList = append(aBreaksList, aBreaks[u]+q)
q++
} else {
q = 0
u += 2
}
}
q, u = 0, 0
var yBreaksList []int
for true {
if u == len(yBreaks) {
break
}
if yBreaks[u]+q != yBreaks[u+1]+1 {
yBreaksList = append(yBreaksList, yBreaks[u]+q)
q++
} else {
q = 0
u += 2
}
}
var p int
for i := 0; i < len(aBreaksList); i++ {
if binarySearch(aBreaksList[i], yBreaksList) == true {
//fmt.Println(aBreaksList[i], "aa")
output++
} else {
boretimeNow++
if boretimeNow == boretime {
p += 2
i = p
}
}
}
//fmt.Println(aBreaksList, yBreaksList)
fmt.Println(output)
}