#include <bits/stdc++.h>
struct trie {
  struct node {
    node* trans[2];
    node() { trans[0] = trans[1] = 0; }
  } * root;
  int ans[60];
  void preset() {
    root = new node;
    memset(ans, 0, sizeof(ans));
  }
  void insert(int x) {
    node* p = root;
    for (int i = 0; i < 32; i++) {
      int k = ((bool)(x & (1 << (31 - i))));
      if (!p->trans[k]) {
        p->trans[k] = new node;
        ans[i + 1]++;
      }
      p = p->trans[k];
    }
  }
  int getans(int k) {
    for (int i = 1; i <= 31; i++)
      if (ans[i] == k) return i;
    return -1;
  }
} tool1;
int n, x1, x2, x3, x4, k, x;
int main() {
  scanf("%d%d", &n, &k);
  tool1.preset();
  for (int i = 1; i <= n; i++) {
    scanf("%d.%d.%d.%d", &x1, &x2, &x3, &x4);
    tool1.insert((x1 << 24) + (x2 << 16) + (x3 << 8) + x4);
  }
  x = tool1.getans(k);
  if (x == -1) {
    printf("-1\n");
    return 0;
  }
  x = ((1 << x) - 1) << (32 - x);
  x1 = (x & 0xff000000) >> 24;
  x2 = (x & 0x00ff0000) >> 16;
  x3 = (x & 0x0000ff00) >> 8;
  x4 = (x & 0x000000ff);
  printf("%d.%d.%d.%d\n", x1, x2, x3, x4);
  return 0;
}
