#include <bits/stdc++.h>
using namespace std;
const int intmax = 0x3f3f3f3f;
const long long lldmax = 0x3f3f3f3f3f3f3f3fll;
double eps = 1e-8;
template <class T>
inline void checkmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline T lowbit(T n) {
  return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
  return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T checkmod(T n, T m) {
  return (n % m + m) % m;
}
template <class T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T euclid(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclid(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclid(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclid(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <class T>
inline int dblcmp(T a, const T b) {
  a -= b;
  return a > eps ? 1 : (a < -eps ? -1 : 0);
}
template <class T>
inline int sgn(T a) {
  return a > eps ? 1 : (a < -eps ? -1 : 0);
}
const int N = 110000;
long long ip[N] = {0};
int n, k;
int count(long long mask) {
  long long last = -1;
  int ans = 0, cnt = 0;
  for (int i = 0; i < n; ++i) {
    if ((ip[i] & mask) == last) {
    } else {
      last = ip[i] & mask;
      ans++;
    }
  }
  return ans;
}
int main() {
  int a;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 4; ++j) {
      scanf("%d", &a);
      getchar();
      ip[i] = (ip[i] << 8) | a;
    }
  }
  sort(ip, ip + n);
  long long mask = (1LL << 32) - 1;
  long long tmp = -1;
  for (int i = 0; i < 31; ++i) {
    mask ^= 1LL << i;
    if (count(mask) == k) {
      tmp = mask;
    }
  }
  if (tmp == -1) {
    puts("-1");
    return 0;
  }
  int ans[4];
  mask = tmp;
  for (int i = 3; i >= 0; --i) {
    ans[i] = mask & 255;
    mask >>= 8;
  }
  printf("%d.%d.%d.%d\n", ans[0], ans[1], ans[2], ans[3]);
  return 0;
}
