#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
int main(int argc, char **argv) {
  const unsigned int nmin = 1, nmax = 100000, mmax = 4;
  unsigned int n, m, mt, res, rt, i, j, k, t, ax;
  unsigned int i0, i1, i2, i3, a, b;
  vector<unsigned int> v;
  scanf("%lu %lu", &n, &k);
  v.resize(n);
  for (i = 0; i < n; i++) {
    scanf("%lu.%lu.%lu.%lu", &i0, &i1, &i2, &i3);
    v[i] = (i0 << 24) + (i1 << 16) + (i2 << 8) + i3;
  }
  sort(v.begin(), v.end());
  ax = 0;
  if (k == 1) {
    rt = 1;
    t = 31;
  } else {
    t = 32;
    rt = 0;
    while (rt + 1 < k) {
      rt += (1 << (32 - t));
      t--;
    }
  }
  rt <<= t;
  while ((ax == 0) && (t >= 0)) {
    ax = 1;
    a = v[0] & rt;
    mt = 1;
    for (i = 1; (i < n) && (mt <= k); i++) {
      b = v[i] & rt;
      if (a < b) mt++;
      a = b;
    }
    if (mt > k)
      ax = 2;
    else if (mt < k)
      ax = 0;
    if (ax != 1) {
      if (t > 0) {
        t--;
        rt += (1 << t);
      } else
        ax = 2;
    }
  }
  if (ax == 1) {
    t = rt;
    i0 = (t >> 24);
    i1 = (t >> 16) & 255;
    i2 = (t >> 8) & 255;
    i3 = t & 255;
    printf("%lu.%lu.%lu.%lu\n", i0, i1, i2, i3);
  } else
    printf("-1\n");
  return 0;
}
