#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void gmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline void gmax(T &a, T b) {
  if (a < b) a = b;
}
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = 1e10;
const double EPS = 1e-9;
const double PI = 3.14159265358979323846264338327950288;
inline int dcmp(const double &a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); }
const int MAXN = 100005;
int n, k;
unsigned s[MAXN];
vector<unsigned> trans(unsigned t) {
  vector<unsigned> ans;
  ans.push_back(t >> 24u & ((1u << 8u) - 1u));
  ans.push_back(t >> 16u & ((1u << 8u) - 1u));
  ans.push_back(t >> 8u & ((1u << 8u) - 1u));
  ans.push_back(t & ((1u << 8u) - 1u));
  return ans;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    unsigned a, b, c, d;
    scanf("%u.%u.%u.%u", &a, &b, &c, &d);
    s[i] = (a << 24u) + (b << 16u) + (c << 8u) + d;
  }
  vector<unsigned> ans;
  unsigned t = ~(0u);
  for (int i = 0; i <= 31; i++) {
    map<int, int> f;
    int cnt = 0;
    for (int j = 1; j <= n; j++) {
      if (f[t & s[j]] == 0) cnt++;
      f[t & s[j]]++;
    }
    if (cnt == k) ans = trans(t);
    t -= (1u << (unsigned)i);
  }
  if (ans.size() == 0)
    cout << -1 << endl;
  else
    cout << ans[0] << "." << ans[1] << "." << ans[2] << "." << ans[3] << endl;
  return 0;
}
