#include <bits/stdc++.h>
using namespace std;
int main() {
  pair<int, int> a[20], b[20];
  int n, m, i, j, flag1, flag2, flag3, ans[20];
  while (cin >> n >> m) {
    flag3 = 0;
    memset(ans, 0, sizeof(ans));
    for (i = 1; i <= n; i++) {
      cin >> a[i].first >> a[i].second;
    }
    for (i = 1; i <= m; i++) {
      cin >> b[i].first >> b[i].second;
    }
    for (i = 1; i <= n; i++) {
      flag1 = 0;
      flag2 = 0;
      for (j = 1; j <= m; j++) {
        if ((a[i].first == b[j].first && a[i].second == b[j].second) ||
            (a[i].first == b[j].second && a[i].second == b[j].first))
          continue;
        else if ((a[i].first == b[j].first && a[i].second != b[j].second) ||
                 (a[i].first == b[j].second && a[i].second != b[j].first) ||
                 (a[i].second == b[j].second && a[i].first != b[j].first) ||
                 (a[i].second == b[j].first && a[i].first != b[j].second)) {
          if ((a[i].first == b[j].first) || (a[i].first == b[j].second)) {
            ans[a[i].first]++;
            if (flag1 == 0)
              flag1 = a[i].first;
            else if (flag2 == 0 && a[i].first != flag1)
              flag2 = a[i].first;
          } else if ((a[i].second == b[j].second) ||
                     (a[i].second == b[j].first)) {
            ans[a[i].second]++;
            if (flag1 == 0)
              flag1 = a[i].second;
            else if (flag2 == 0 && a[i].second != flag1)
              flag2 = a[i].second;
          }
        }
      }
      if (flag1 != 0 && flag2 == 0 && flag3 == 0) flag3 = 1;
      if (flag1 != 0 && flag2 != 0) flag3 = 3;
    }
    for (i = 1; i <= m; i++) {
      flag1 = 0;
      flag2 = 0;
      for (j = 1; j <= n; j++) {
        if ((a[i].first == b[j].first && a[i].second == b[j].second) ||
            (a[i].first == b[j].second && a[i].second == b[j].first))
          continue;
        else if ((b[i].first == a[j].first && b[i].second != a[j].second) ||
                 (b[i].first == a[j].second && b[i].second != a[j].first) ||
                 (b[i].second == a[j].second && b[i].first != a[j].first) ||
                 (b[i].second == a[j].first && b[i].first != a[j].second)) {
          if ((b[i].first == a[j].first) || (b[i].first == a[j].second)) {
            ans[b[i].first]++;
            if (flag1 == 0)
              flag1 = b[i].first;
            else if (flag2 == 0 && b[i].first != flag1)
              flag2 = b[i].first;
          } else if ((b[i].second == a[j].second) ||
                     (b[i].second == a[j].first)) {
            ans[b[i].second]++;
            if (flag1 == 0)
              flag1 = b[i].second;
            else if (flag2 == 0 && b[i].second != flag1)
              flag2 = b[i].second;
          }
        }
      }
      if (flag1 != 0 && flag2 == 0 && flag3 == 1) flag3 = 2;
      if (flag1 != 0 && flag2 != 0) flag3 = 3;
    }
    int num = 0, flag4 = 0;
    for (i = 1; i <= 9; i++) {
      if (ans[i]) num++;
    }
    if (num == 1) {
      for (i = 1; i <= 9; i++) {
        if (ans[i]) cout << i << endl;
        flag4 = 1;
      }
    }
    if (flag3 == 2 && flag4 != 1)
      cout << "0" << endl;
    else if (flag4 != 1)
      cout << "-1" << endl;
  }
  return 0;
}
