#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
int n, m;
int X1[22], X2[22];
int Y1[22], Y2[22];
bool matches(pair<int, int> F, pair<int, int> S) {
  if (F.first != S.first && F.first != S.second && F.second != S.first &&
      F.second != S.second)
    return false;
  if (F == S) return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = 0; (i) < (n); (i)++) {
    scanf("%d%d", X1 + i, Y1 + i);
    int a = X1[i], b = Y1[i];
    X1[i] = min(a, b);
    Y1[i] = max(a, b);
  }
  for (int(i) = 0; (i) < (m); (i)++) {
    scanf("%d%d", X2 + i, Y2 + i);
    int a = X2[i], b = Y2[i];
    X2[i] = min(a, b);
    Y2[i] = max(a, b);
  }
  set<int> poss;
  bool firstKnew = true, secKnew = true;
  for (int(i) = 0; (i) < (n); (i)++)
    for (int(j) = 0; (j) < (m); (j)++) {
      if (!matches(make_pair(X1[i], Y1[i]), make_pair(X2[j], Y2[j]))) continue;
      int sharedNum = (X1[i] == X2[j] || X1[i] == Y2[j]) ? X1[i] : Y1[i];
      poss.insert(sharedNum);
      set<int> hisPos;
      for (int(k) = 0; (k) < (m); (k)++) {
        if (!matches(make_pair(X1[i], Y1[i]), make_pair(X2[k], Y2[k])))
          continue;
        int sN = (X1[i] == X2[k] || X1[i] == Y2[k]) ? X1[i] : Y1[i];
        hisPos.insert(sN);
      }
      if (hisPos.size() > 1) firstKnew = false;
      hisPos.clear();
      for (int(k) = 0; (k) < (n); (k)++) {
        if (!matches(make_pair(X1[k], Y1[k]), make_pair(X2[j], Y2[j])))
          continue;
        int sN = (X1[k] == X2[j] || X1[k] == Y2[j]) ? X1[k] : Y1[k];
        hisPos.insert(sN);
      }
      if (hisPos.size() > 1) secKnew = false;
    }
  if (poss.size() == 1)
    printf("%d\n", *poss.begin());
  else if (firstKnew && secKnew)
    printf("%d\n", 0);
  else
    printf("-1");
  return 0;
}
