#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> s[2][20];
vector<pair<int, int> > good_choices;
set<int> good;
bool intersect(pair<int, int> a, pair<int, int> b, int &val) {
  val = -1;
  if (a.first == b.first) {
    val = a.first;
  }
  if (a.first == b.second) {
    if (val != -1) {
      return false;
    }
    val = a.first;
  }
  if (a.second == b.first) {
    if (val != -1) {
      return false;
    }
    val = a.second;
  }
  if (a.second == b.second) {
    if (val != -1) {
      return false;
    }
    val = a.second;
  }
  if (val == -1) return false;
  return true;
}
void find_good() {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int val;
      if (intersect(s[0][i], s[1][j], val)) {
        ;
        good.insert(val);
      }
    }
  }
}
bool check2() {
  for (int i = 0; i < n; ++i) {
    set<int> all;
    for (int j = 0; j < m; ++j) {
      int val;
      if (intersect(s[0][i], s[1][j], val)) {
        all.insert(val);
      }
    };
    for (int x : all) {
      ;
    };
    if (((int)(all).size()) > 1) {
      return false;
    }
  }
  for (int j = 0; j < m; ++j) {
    set<int> all;
    for (int i = 0; i < n; ++i) {
      int val;
      if (intersect(s[0][i], s[1][j], val)) {
        all.insert(val);
      }
    }
    if (((int)(all).size()) > 1) {
      return false;
    }
  }
  return true;
}
void mmain() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &s[0][i].first, &s[0][i].second);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &s[1][i].first, &s[1][i].second);
  }
  find_good();
  if (((int)(good).size()) == 1) {
    printf("%d\n", *good.begin());
    return;
  }
  if (check2()) {
    printf("0\n");
    return;
  }
  printf("-1\n");
}
int main() { mmain(); }
