#include <bits/stdc++.h>
using namespace std;
const int maxn = 12;
int cnt[2];
bool zero = true;
vector<pair<int, int> > seq[2];
set<int> ans;
void Solve(int cur) {
  for (int i = 0; i < seq[cur].size(); i++) {
    bool fi = false, se = false;
    for (int j = 0; j < seq[cur ^ 1].size(); j++) {
      int t = 0, p;
      if (seq[cur][i].first == seq[cur ^ 1][j].first)
        t++, p = seq[cur][i].first;
      if (seq[cur][i].first == seq[cur ^ 1][j].second)
        t++, p = seq[cur][i].first;
      if (seq[cur][i].second == seq[cur ^ 1][j].first)
        t++, p = seq[cur][i].second;
      if (seq[cur][i].second == seq[cur ^ 1][j].second)
        t++, p = seq[cur][i].second;
      if (t == 1) {
        if (p == seq[cur][i].first) {
          if (se) zero = false;
          fi = true;
        } else {
          if (fi) zero = false;
          se = true;
        }
        ans.insert(p);
      }
    }
  }
}
int main() {
  for (int i = 0; i < 2; i++) scanf("%d", &cnt[i]);
  for (int j = 0; j < 2; j++) {
    for (int i = 0, x, y; i < cnt[j]; i++)
      scanf("%d%d", &x, &y), seq[j].push_back(pair<int, int>(x, y));
  }
  Solve(0);
  Solve(1);
  if (ans.size() == 1)
    printf("%d\n", *ans.begin());
  else
    printf("%d\n", zero ? 0 : -1);
  return 0;
}
