#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int T, n, m;
int t1, t2;
class ComboPair {
 public:
  int val[2];
  vector<int> ch[2];
};
ComboPair vp1[15];
ComboPair vp2[15];
set<int> ones;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &t1, &t2);
    if (t1 > t2) swap(t1, t2);
    vp1[i].val[0] = t1;
    vp1[i].val[1] = t2;
  }
  for (int x = 0; x < m; ++x) {
    scanf("%d %d", &t1, &t2);
    if (t1 > t2) swap(t1, t2);
    vp2[x].val[0] = t1;
    vp2[x].val[1] = t2;
    for (int i = 0; i < n; ++i) {
      int cc = 0, tc1, tc2;
      for (int c1 = 0; c1 < 2; ++c1) {
        for (int c2 = 0; c2 < 2; ++c2) {
          if (vp2[x].val[c1] == vp1[i].val[c2]) {
            ++cc;
            tc1 = c1, tc2 = c2;
          }
        }
      }
      if (cc == 1) {
        vp2[x].ch[tc1].push_back(i);
        vp1[i].ch[tc2].push_back(x);
        ones.insert(vp2[x].val[tc1]);
      }
    }
  }
  bool two = false;
  for (int i = 0; i < n; ++i) {
    if (vp1[i].ch[0].size() > 0 && vp1[i].ch[1].size() > 0) {
      two = true;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (vp2[i].ch[0].size() > 0 && vp2[i].ch[1].size() > 0) {
      two = true;
    }
  }
  if (two)
    printf("-1\n");
  else if (ones.size() > 1)
    printf("0\n");
  else
    printf("%d\n", (*ones.begin()));
}
