#include <bits/stdc++.h>
using namespace std;
unsigned int inet4[100010];
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    char* str;
    int inet[4];
    scanf("%d.%d.%d.%d", &inet[0], &inet[1], &inet[2], &inet[3]);
    inet4[i] = 0;
    for (int j = 0; j < 4; j++) inet4[i] = inet4[i] * 256 + inet[j];
  }
  unsigned int mask = 0x80000000;
  bool flag = false;
  for (int i = 1; i < 32; i++) {
    set<int> hh;
    for (int j = 0; j < n; j++) {
      hh.insert(inet4[j] & mask);
    }
    if (hh.size() == m) {
      int ans[4];
      for (int j = 3; j >= 0; j--) {
        ans[j] = mask % 256;
        mask /= 256;
      }
      printf("%d.%d.%d.%d\n", ans[0], ans[1], ans[2], ans[3]);
      flag = true;
      break;
    }
    mask = mask | (mask >> 1);
  }
  if (!flag) printf("-1\n");
  return 0;
}
