package main
import (
"fmt"
)
type pair struct {
x, y int
}
func shared(a, b pair) int {
count := 0
val := 0
if a.x == b.x {
count++
val = a.x
}
if a.x == b.y {
count++
val = a.x
}
if a.y == b.x {
count++
val = a.y
}
if a.y == b.y {
count++
val = a.y
}
if count == 1 {
return val
}
return 0
}
func main() {
var n, m int
if _, err := fmt.Scan(&n, &m); err != nil {
return
}
A := make([]pair, n)
for i := 0; i < n; i++ {
fmt.Scan(&A[i].x, &A[i].y)
}
B := make([]pair, m)
for i := 0; i < m; i++ {
fmt.Scan(&B[i].x, &B[i].y)
}
VA := make(map[int]map[int]bool)
VB := make(map[int]map[int]bool)
for i := 0; i < n; i++ {
VA[i] = make(map[int]bool)
}
for i := 0; i < m; i++ {
VB[i] = make(map[int]bool)
}
for i, a := range A {
for j, b := range B {
s := shared(a, b)
if s > 0 {
VA[i][s] = true
VB[j][s] = true
}
}
}
allShared := make(map[int]bool)
for i, a := range A {
for j, b := range B {
s := shared(a, b)
if s > 0 {
if len(VA[i]) > 1 || len(VB[j]) > 1 {
fmt.Println("-1")
return
}
allShared[s] = true
}
}
}
if len(allShared) == 1 {
for k := range allShared {
fmt.Println(k)
return
}
} else if len(allShared) > 1 {
fmt.Println("0")
} else {
fmt.Println("-1")
}
}