#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> a[200];
pair<int, int> b[200];
int pp[200];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int aa, bb;
    scanf("%d%d", &aa, &bb);
    a[i].first = min(aa, bb), a[i].second = max(aa, bb);
  }
  for (int i = 1; i <= m; i++) {
    int aa, bb;
    scanf("%d%d", &aa, &bb);
    b[i].first = min(aa, bb), b[i].second = max(aa, bb);
  }
  for (int i = 1; i <= n; i++) {
    bool flag1 = false, flag2 = false;
    for (int j = 1; j <= m; j++) {
      if (a[i].first != b[j].first && a[i].second == b[j].second)
        pp[b[j].second] = 1, flag1 = true;
      if (a[i].second != b[j].second && a[i].first == b[j].first)
        pp[b[j].first] = 1, flag2 = true;
      if (a[i].second != b[j].first && a[i].first == b[j].second)
        pp[b[j].second] = 1, flag2 = true;
      if (a[i].first != b[j].second && a[i].second == b[j].first)
        pp[b[j].first] = 1, flag1 = true;
      if (flag1 && flag2 && a[i].first != a[i].second) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    bool flag1 = false, flag2 = false;
    for (int j = 1; j <= n; j++) {
      if (a[j].first != b[i].first && a[j].second == b[i].second) flag2 = true;
      if (a[j].second != b[i].second && a[j].first == b[i].first) flag1 = true;
      if (a[j].second != b[i].first && a[j].first == b[i].second) flag2 = true;
      if (a[j].first != b[i].second && a[j].second == b[i].first) flag1 = true;
      if (flag1 && flag2 && b[i].first != b[i].second) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int sum = 0;
  int ans;
  for (int i = 1; i <= 9; i++) {
    if (pp[i]) ans = i;
    sum += pp[i];
  }
  if (sum == 1) {
    cout << ans << endl;
    return 0;
  } else {
    cout << 0 << endl;
    return 0;
  }
  return 0;
}
