#include <bits/stdc++.h>
using namespace std;
struct abc {
  int a, b, c, d;
};
abc w[100010];
int p[35][100010], z[100010];
bool f(const abc &a, const abc &b) {
  if (a.a < b.a) return 0;
  if (a.a > b.a) return 1;
  if (a.b < b.b) return 0;
  if (a.b > b.b) return 1;
  if (a.c < b.c) return 0;
  if (a.c > b.c) return 1;
  if (a.d < b.d) return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, k, a, odp = 1, aw, ai;
  string c;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ai = 0;
    cin >> c;
    aw = 0;
    while (ai < c.size() && c[ai] != '.') {
      aw *= 10;
      aw += (c[ai] - '0');
      ai++;
    }
    w[i].a = aw;
    aw = 0;
    ai++;
    while (ai < c.size() && c[ai] != '.') {
      aw *= 10;
      aw += (c[ai] - '0');
      ai++;
    }
    w[i].b = aw;
    aw = 0;
    ai++;
    while (ai < c.size() && c[ai] != '.') {
      aw *= 10;
      aw += (c[ai] - '0');
      ai++;
    }
    w[i].c = aw;
    aw = 0;
    ai++;
    while (ai < c.size()) {
      aw *= 10;
      aw += (c[ai] - '0');
      ai++;
    }
    w[i].d = aw;
  }
  sort(w, w + n, f);
  for (int i = 0; i < n; i++) {
    a = w[i].a;
    for (int j = 0; j < 8; j++) {
      if (a & (1 << (8 - j - 1))) {
        p[j][i] = 1;
      }
    }
    a = w[i].b;
    for (int j = 0; j < 8; j++) {
      if (a & (1 << (8 - j - 1))) {
        p[j + 8][i] = 1;
      }
    }
    a = w[i].c;
    for (int j = 0; j < 8; j++) {
      if (a & (1 << (8 - j - 1))) {
        p[j + 16][i] = 1;
      }
    }
    a = w[i].d;
    for (int j = 0; j < 8; j++) {
      if (a & (1 << (8 - j - 1))) {
        p[j + 24][i] = 1;
      }
    }
  }
  for (int i = 0; i < 32; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (p[i][j] != p[i][j + 1] && z[j] == 0) {
        odp++;
        z[j] = 1;
      }
    }
    if (odp == k) {
      int y;
      i++;
      if (i) {
        i--;
        y = 1;
      } else
        y = 0;
      while (i && y < 128) {
        y = y * 2 + 1;
        i--;
      }
      while (y && y < 128) y *= 2;
      cout << y << ".";
      if (i) {
        i--;
        y = 1;
      } else
        y = 0;
      while (i && y < 128) {
        y = y * 2 + 1;
        i--;
      }
      while (y && y < 128) y *= 2;
      cout << y << ".";
      if (i) {
        i--;
        y = 1;
      } else
        y = 0;
      while (i && y < 128) {
        y = y * 2 + 1;
        i--;
      }
      while (y && y < 128) y *= 2;
      cout << y << ".";
      if (i) {
        i--;
        y = 1;
      } else
        y = 0;
      while (i && y < 128) {
        y = y * 2 + 1;
        i--;
      }
      while (y && y < 128) y *= 2;
      cout << y;
      return 0;
    }
  }
  cout << "-1";
  return 0;
}
