#include <bits/stdc++.h>
using namespace std;
const int maxN = 200000;
int a[maxN][40];
int b[40 * maxN], nw[40 * maxN];
int n, k;
void add(int i, int x, int l, int r) {
  for (int j = r; j >= l; j--) {
    a[i][j] = x % 2;
    x /= 2;
  }
}
void no() {
  cout << -1;
  exit(0);
}
void ans(int x) {
  int cur = 0;
  int st = 1 << 7;
  for (int i = 1; i <= 32; i++) {
    if (i <= x) cur += st;
    st = st / 2;
    if (i == 8 || i == 16 || i == 24 || i == 32) {
      cout << cur;
      if (i != 32) cout << '.';
      cur = 0;
      st = 1 << 7;
    }
  }
  exit(0);
}
void wr(int i) {
  for (int j = 1; j <= 32; j++) cout << a[i][j];
  cout << endl;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    b[i] = 1;
    int x;
    char ch;
    cin >> x >> ch;
    add(i, x, 1, 8);
    cin >> x >> ch;
    add(i, x, 9, 16);
    cin >> x >> ch;
    add(i, x, 17, 24);
    cin >> x;
    add(i, x, 25, 32);
  }
  int kol = 1;
  int an = 1;
  for (int kl = 1; kl < 32; kl++) {
    for (int i = 1; i <= kol; i++) nw[i] = 0;
    for (int i = 1; i <= n; i++)
      if (a[i][kl] == 1) {
        if (nw[b[i]] == 0) {
          kol++;
          nw[b[i]] = kol;
        }
        b[i] = nw[b[i]];
      }
    for (int i = 1; i <= kol; i++) nw[i] = 0;
    an = 0;
    for (int i = 1; i <= n; i++)
      if (nw[b[i]] == 0) {
        nw[b[i]] = 1;
        an++;
      }
    if (an == k) ans(kl);
    if (an > k) no();
  }
  if (an < k) no();
  return 0;
}
