#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int n, m, inf = 1000000000;
char c;
vector<int> v[110];
int uk[110];
int main() {
  cout << setiosflags(ios::fixed) << setprecision(10);
  memset(uk, 0, sizeof(uk));
  cin >> n >> m;
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      cin >> c;
      if (c == '1') {
        v[i].push_back(j);
        v[i].push_back(j + m);
      }
    }
    if (v[i].size() == 0) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 0; i < (n); i++) sort(v[i].begin(), v[i].end());
  int ans = inf;
  for (int i = 0; i < (2 * m); i++) {
    int d = 0;
    for (int j = 0; j < (n); j++) {
      int d1 = abs(v[j][uk[j]] - i);
      int d2;
      if (uk[j] < v[j].size() - 1)
        d2 = abs(v[j][uk[j] + 1] - i);
      else
        d2 = inf;
      if (d2 < d1) {
        d += d2;
        uk[j]++;
      } else
        d += d1;
    }
    ans = min(ans, d);
  }
  cout << ans;
  return 0;
}
