#include <bits/stdc++.h>
using namespace std;
int i, j, n, m, mas1[2000][3], mas2[2000][3], x1, x2, ab[10000][3], r, x[1000],
    aa;
set<int> s;
int main() {
  std::cin.sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m;
  i = 0;
  while (i < n) {
    cin >> mas1[i][0] >> mas1[i][1];
    i++;
  }
  i = 0;
  while (i < m) {
    cin >> mas2[i][0] >> mas2[i][1];
    i++;
  }
  i = 0;
  while (i < n) {
    j = 0;
    while (j < m) {
      if (mas1[i][0] == mas2[j][0] && mas1[i][1] == mas2[j][1]) {
      } else if (mas1[i][0] == mas2[j][1] && mas1[i][1] == mas2[j][0]) {
      } else if (mas1[i][0] == mas2[j][0]) {
        ab[r][0] = i;
        ab[r][1] = j;
        ab[r][2] = mas1[i][0];
        mas1[i][2] = 1;
        mas2[j][2] = 1;
        r++;
      } else if (mas1[i][1] == mas2[j][1]) {
        ab[r][0] = i;
        ab[r][1] = j;
        ab[r][2] = mas1[i][1];
        mas1[i][2] = 1;
        mas2[j][2] = 1;
        r++;
      } else if (mas1[i][0] == mas2[j][1]) {
        ab[r][0] = i;
        ab[r][1] = j;
        ab[r][2] = mas1[i][0];
        mas1[i][2] = 1;
        mas2[j][2] = 1;
        r++;
      } else if (mas1[i][1] == mas2[j][0]) {
        ab[r][0] = i;
        ab[r][1] = j;
        ab[r][2] = mas1[i][1];
        mas1[i][2] = 1;
        mas2[j][2] = 1;
        r++;
      }
      j++;
    }
    i++;
  }
  i = 0;
  while (i < r) {
    x[ab[i][2]]++;
    i++;
  }
  i = 0;
  aa = 0;
  while (i < 20) {
    if (x[i] > 0) {
      s.insert(i);
    }
    i++;
  }
  if (s.size() == 1) {
    cout << *s.begin();
  } else {
    i = 0;
    while (i < n) {
      x1 = 0;
      x2 = 0;
      j = 0;
      while (j < r) {
        if (ab[j][0] == i && ab[j][2] == mas1[i][0]) {
          x1 = 1;
        }
        if (ab[j][0] == i && ab[j][2] == mas1[i][1]) {
          x2 = 1;
        }
        j++;
      }
      if (x1 == 1 && x2 == 1) {
        aa = 1;
      }
      i++;
    }
    i = 0;
    while (i < m) {
      x1 = 0;
      x2 = 0;
      j = 0;
      while (j < r) {
        if (ab[j][1] == i && ab[j][2] == mas2[i][0]) {
          x1 = 1;
        }
        if (ab[j][1] == i && ab[j][2] == mas2[i][1]) {
          x2 = 1;
        }
        j++;
      }
      if (x1 == 1 && x2 == 1) {
        aa = 1;
      }
      i++;
    }
    if (aa == 0) {
      cout << "0";
    } else {
      cout << "-1";
    }
  }
  return 0;
}
