#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct coord {
  int c[7];
};
bool operator<(coord left, coord right) {
  if (left.c[0] < right.c[0]) return true;
  if (left.c[0] > right.c[0]) return false;
  if (left.c[1] < right.c[1]) return true;
  if (left.c[1] > right.c[1]) return false;
  if (left.c[2] < right.c[2]) return true;
  if (left.c[2] > right.c[2]) return false;
  if (left.c[3] < right.c[3])
    return true;
  else
    return false;
}
coord a[N], b[N];
char s[100];
int i, as[60], p, cr, j, e, f, re, t[15], top, n, k, pa[15];
int main() {
  as[0] = 1;
  for (i = 1; i <= 30; ++i) as[i] = as[i - 1] * 2;
  for (i = 7; i >= 0; --i) {
    cr = cr + as[i];
    top++;
    t[top] = cr;
  }
  scanf("%d%d", &n, &k);
  for (i = 1; i <= n; ++i) {
    s[0] = '\0';
    scanf("%s", &s);
    p = strlen(s);
    cr = 0;
    for (j = 0; j < p; ++j) {
      if (s[j] == '.') {
        cr++;
        continue;
      }
      a[i].c[cr] = a[i].c[cr] * 10 + s[j] - '0';
    }
  }
  for (j = 0; j < 4; ++j) {
    for (e = 1; e <= top; ++e) {
      for (i = 0; i < j; ++i) pa[i] = 255;
      for (i = j + 1; i < 4; ++i) pa[i] = 0;
      pa[j] = t[e];
      for (i = 1; i <= n; ++i) {
        for (f = 0; f < 4; ++f) {
          b[i].c[f] = (a[i].c[f] & pa[f]);
        }
      }
      sort(b + 1, b + n + 1);
      re = 0;
      for (i = 1; i <= n; ++i) {
        if (i + 1 <= n && b[i].c[0] == b[i + 1].c[0] &&
            b[i].c[1] == b[i + 1].c[1] && b[i].c[2] == b[i + 1].c[2] &&
            b[i].c[3] == b[i + 1].c[3])
          continue;
        re++;
      }
      if (re == k) {
        printf("%d.%d.%d.%d\n", pa[0], pa[1], pa[2], pa[3]);
        return 0;
      }
    }
  }
  printf("-1\n");
  return 0;
}
