#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const long long inf = 1e18 + 7;
typedef struct {
  int x, y;
} po;
po poi[20], yud[20];
int vis[3010];
int pre[5010];
int a, b, n, k, c, book, m, P, x, y, t, q, cnt, book1, book2;
long long ans;
int dit[5][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}};
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) cin >> poi[i].x >> poi[i].y;
    for (int i = 1; i <= m; i++) cin >> yud[i].x >> yud[i].y;
    book = 0;
    ans = 1;
    for (int i = 1; i <= n; i++) {
      book1 = 0, book2 = 0;
      for (int j = 1; j <= m; j++) {
        if ((min(poi[i].x, poi[i].y) == min(yud[j].x, yud[j].y)) &&
            (max(poi[i].x, poi[i].y) == max(yud[j].x, yud[j].y)))
          continue;
        if (poi[i].x == yud[j].x || poi[i].x == yud[j].y) book1 = 1;
        if (poi[i].y == yud[j].x || poi[i].y == yud[j].y) book2 = 1;
      }
      if (book1 && book2) {
        ans = -1;
        break;
      } else if (ans == 1 && book1) {
        if (!book)
          book = poi[i].x;
        else if (book != poi[i].x)
          ans = 0;
      } else if (ans == 1 && book2) {
        if (!book)
          book = poi[i].y;
        else if (book != poi[i].y)
          ans = 0;
      }
    }
    for (int i = 1; i <= m; i++) {
      book1 = 0, book2 = 0;
      for (int j = 1; j <= n; j++) {
        if ((min(yud[i].x, yud[i].y) == min(poi[j].x, poi[j].y)) &&
            (max(yud[i].x, yud[i].y) == max(poi[j].x, poi[j].y)))
          continue;
        if (yud[i].x == poi[j].x || yud[i].x == poi[j].y) book1 = 1;
        if (yud[i].y == poi[j].x || yud[i].y == poi[j].y) book2 = 1;
      }
      if (book1 && book2) {
        ans = -1;
        break;
      } else if (ans == 1 && book1) {
        if (!book)
          book = yud[i].x;
        else if (book != yud[i].x)
          ans = 0;
      } else if (ans == 1 && book2) {
        if (!book)
          book = yud[i].y;
        else if (book != yud[i].y)
          ans = 0;
      }
    }
    if (ans == 1)
      cout << book << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
