#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
pair<int, int> a[15], b[15];
set<int> s1[15], s2[15];
set<int> ans;
int n, m;
int main() {
  int cnt = 0, p = 0;
  bool can = true;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (n); ++i) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 0; i < (m); ++i) scanf("%d%d", &b[i].first, &b[i].second);
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (m); ++j) {
      if (a[i].first == b[j].first && a[i].second != b[j].second) {
        s1[i].insert(a[i].first);
        s2[j].insert(a[i].first);
        ans.insert(a[i].first);
      } else if (a[i].first == b[j].second && a[i].second != b[j].first) {
        s1[i].insert(a[i].first);
        s2[j].insert(a[i].first);
        ans.insert(a[i].first);
      } else if (a[i].first != b[j].first && a[i].second == b[j].second) {
        s1[i].insert(a[i].second);
        s2[j].insert(a[i].second);
        ans.insert(a[i].second);
      } else if (a[i].first != b[j].second && a[i].second == b[j].first) {
        s1[i].insert(a[i].second);
        s2[j].insert(a[i].second);
        ans.insert(a[i].second);
      }
    }
  }
  if (ans.size() == 1)
    printf("%d\n", *(ans.begin()));
  else {
    for (int i = 0; i < (n); ++i)
      if (s1[i].size() > 1) can = false;
    for (int i = 0; i < (m); ++i)
      if (s2[i].size() > 1) can = false;
    if (can)
      puts("0");
    else
      puts("-1");
  }
  return 0;
}
