#include <bits/stdc++.h>
using namespace std;
char a[20005];
int n;
int m;
int left1[20005];
int right1[20005];
int res[10005];
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int main() {
  cin >> n >> m;
  for (int run = 0; run < n; run++) {
    cin >> a;
    left1[0] = -1;
    if (a[0] == '1') {
      left1[0] = 0;
    } else {
      for (int j = m - 1; j > 0; j--) {
        if (a[j] == '1') {
          left1[0] = m - j;
          break;
        }
      }
    }
    if (left1[0] == -1) {
      cout << "-1" << endl;
      return 0;
    }
    for (int j = 1; j < m; j++) {
      if (a[j] == '1') {
        left1[j] = 0;
      } else {
        left1[j] = left1[j - 1] + 1;
      }
    }
    if (a[m - 1] == '1') {
      right1[m - 1] = 0;
    } else {
      for (int j = 0; j < m - 1; j++) {
        if (a[j] == '1') {
          right1[m - 1] = j + 1;
          break;
        }
      }
    }
    for (int j = m - 2; j >= 0; j--) {
      if (a[j] == '1') {
        right1[j] = 0;
      } else {
        right1[j] = right1[j + 1] + 1;
      }
    }
    for (int j = 0; j < m; j++) {
      res[j] += min(right1[j], left1[j]);
    }
  }
  cout << *min_element(res, res + m);
  return 0;
}
