#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = 3.1415926535897932384626433832795;
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
int k, n;
char buf[100];
vector<long long> a;
long long get(string s) {
  s += ".";
  int now = 0;
  long long res = 0;
  for (int i = 0; i < (int)(s.size()); i++) {
    if (s[i] == '.') {
      res = (res << 8) + now;
      now = 0;
    } else
      now = now * 10 + s[i] - '0';
  }
  return res;
}
void write(long long x) {
  if (x == -1) {
    cout << x << endl;
    exit(0);
  }
  vector<int> res;
  for (int i = 0; i < (int)(4); i++) {
    res.push_back(x % (1 << 8));
    x >>= 8;
  }
  for (int i = (int)(res.size()) - 1; i >= 0; i--) {
    cout << res[i];
    if (i > 0)
      cout << ".";
    else
      cout << endl;
  }
  exit(0);
}
bool ls(long long a, long long b) {
  long long c = a ^ b;
  while ((c & (c - 1)) != 0) c = c & (c - 1);
  if (a & c) return 1;
  return 0;
}
int main() {
  cin >> n >> k;
  gets(buf);
  for (int i = 0; i < (int)(n); i++) {
    gets(buf);
    string s = buf;
    long long now = get(s);
    a.push_back(now);
  }
  sort(a.begin(), a.end(), ls);
  long long res = 0;
  for (int i = (int)(32) - 1; i >= 0; i--) {
    res += 1ll << i;
    int now = 1;
    for (int j = 0; j < (int)(n - 1); j++)
      if ((a[j] & res) != (a[j + 1] & res)) now++;
    if (now == k) write(res);
    if (now > k) write(-1);
  }
  write(-1);
  return 0;
}
