#include <bits/stdc++.h>
using namespace std;
vector<int> m1;
vector<int> m2;
int n, m;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    int c = 0;
    c |= (1 << a);
    c |= (1 << b);
    m1.push_back(c);
  }
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b;
    c = 0;
    c |= (1 << a);
    c |= (1 << b);
    m2.push_back(c);
  }
  set<int> hmm;
  for (auto v : m1) {
    int ans = 0;
    for (auto u : m2) {
      int res = 0;
      if (u == v) continue;
      res = v & u;
      if (__builtin_popcount(res) == 1) {
        bool fuck = true;
        ans = res;
        res = 0;
        for (auto k : m2) {
          if (k == v) continue;
          res |= k & v;
        }
        if (__builtin_popcount(res) == 2) {
          fuck = false;
        }
        res = 0;
        for (auto z : m1) {
          if (z == u) continue;
          res |= u & z;
        }
        if (__builtin_popcount(res) == 2) {
          fuck = false;
        }
        if (fuck) {
          hmm.insert(ans);
        } else {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  if (hmm.size() == 1) {
    for (int i = 0; i < 10; i++) {
      if (*hmm.begin() & (1 << i)) {
        printf("%d\n", i);
        return 0;
      }
    }
  } else if (hmm.size() > 1) {
    printf("0\n");
  } else
    printf("-1\n");
  return 0;
}
