#include <bits/stdc++.h>
using namespace std;
char s[11000];
int ans[11000];
int a[11000], b[11000];
int n, m;
int main() {
  cin >> n >> m;
  bool over = 0;
  for (int i = (1); i <= (n); i++) {
    scanf("%s", s + 1);
    for (int i = (1); i <= (m); i++)
      if (s[i] == '1')
        a[i] = i;
      else
        a[i] = a[i - 1];
    for (int i = (m); i >= (1); i--)
      if (s[i] == '1')
        b[i] = i;
      else
        b[i] = b[i + 1];
    if (a[m] == 0) {
      over = 1;
      break;
    }
    for (int i = (1); i <= (m); i++) {
      int ret = min(m - i + b[1], m - a[m] + i);
      if (a[i]) ret = min(ret, i - a[i]);
      if (b[i]) ret = min(ret, b[i] - i);
      ans[i] += ret;
    }
  }
  if (over)
    puts("-1");
  else
    cout << *min_element(ans + 1, ans + m + 1) << endl;
  return 0;
}
