#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, res, k;
long long id[N];
string str;
bool get(int l, int r, int idx) {
  if (l == r) return false;
  if (l + 1 < r && str[l] == '0') return false;
  int s = 0;
  for (int i = l; i < r; ++i) {
    if (str[i] > '9' || str[i] < '0') return false;
    s = s * 10 + str[i] - '0';
    if (s > 255) return false;
  }
  id[idx] = id[idx] * 256 + s;
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> str;
    int pre = 0, len = str.size(), ok = 1, now = 0;
    for (int j = 0; j < len; ++j) {
      if (str[j] == '.') {
        if (now > 3 || !get(pre, j, i)) {
          ok = 0;
          break;
        }
        pre = j + 1;
        ++now;
      }
    }
    if (now > 3 || !get(pre, len, i)) ok = 0;
    if (!ok) {
      --n, id[i--] = 0;
    }
  }
  long long res = -1, mask = (1LL << 32) - 1;
  for (int i = 0; i < 32; ++i) {
    set<long long> second;
    for (int j = 0; j < n; ++j) {
      second.insert(id[j] & mask);
    }
    if (second.size() == k) res = mask;
    mask -= (1LL << i);
  }
  if (~res) {
    int r[4];
    for (int i = 0; i < 4; ++i, res >>= 8) r[i] = (res & 255);
    for (int i = 3; i > -1; --i) cout << r[i] << (i ? '.' : '\n');
  } else
    cout << -1 << endl;
  return 0;
}
