#include <bits/stdc++.h>
using namespace std;
int A[100][10000];
int d[100][10000];
int main() {
  int n, m;
  cin >> n >> m;
  string str;
  getline(cin, str);
  for (int i = 0; i < n; i++) {
    getline(cin, str);
    for (int j = 0; j < str.size(); j++)
      if (str[j] == '1')
        A[i][j] = 1;
      else
        A[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    int prev = 1000001;
    int prev2 = 100000;
    for (int j = m - 1; j >= 0; j--)
      if (A[i][j]) {
        prev = -(m - j);
        break;
      }
    if (prev == 1000001) {
      cout << -1 << endl;
      return 0;
    }
    bool first = true;
    for (int j = 0; j < m; j++) {
      if (A[i][j]) {
        if (first) {
          prev2 = j + 1 + m - 1;
          first = false;
        }
        d[i][j] = 0;
        prev = j;
      } else
        d[i][j] = j - prev;
    }
    for (int j = m - 1; j >= 0; j--) {
      d[i][j] = min(d[i][j], prev2 - j);
      if (A[i][j]) prev2 = j;
    }
  }
  int mindall = 1000000;
  for (int i = 0; i < m; i++) {
    int mind = 0;
    for (int j = 0; j < n; j++) {
      mind += d[j][i];
    }
    mindall = min(mindall, mind);
  }
  cout << mindall << endl;
  return 0;
}
