#include <bits/stdc++.h>
using namespace std;
int dp[101][10001];
int main() {
  int i, j, n, m;
  vector<int> g[1000];
  vector<int>::iterator it;
  string s[101];
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = (int)0; i < (int)n; ++i) {
    cin >> s[i];
    for (int j = (int)0; j < (int)int(s[i].size()); ++j) {
      if (s[i][j] == '1') g[i].push_back(j);
    }
    if (int(g[i].size()) == 0) {
      printf("-1\n");
      return 0;
    }
  }
  int x, y, in;
  for (int i = (int)0; i < (int)n; ++i) {
    for (int j = (int)0; j < (int)m; ++j) {
      if (s[i][j] == '1')
        dp[i][j] = 0;
      else {
        it = upper_bound(g[i].begin(), g[i].end(), j);
        in = it - g[i].begin();
        if (in > 0) in--;
        x = g[i][in];
        y = min(abs(j - x), m - (abs(j - x)));
        dp[i][j] = y;
        if (in < int(g[i].size()) - 1) {
          x = g[i][in + 1];
          y = min(abs(j - x), m - abs(j - x));
          dp[i][j] = min(y, dp[i][j]);
        }
        x = g[i][0];
        dp[i][j] = min(dp[i][j], m - abs(j - x));
        x = g[i].back();
        dp[i][j] = min(dp[i][j], m - abs(j - x));
      }
    }
  }
  int sum = 0, ans = INT_MAX;
  for (int i = (int)0; i < (int)m; ++i) {
    sum = 0;
    for (int j = (int)0; j < (int)n; ++j) sum += dp[j][i];
    ans = min(ans, sum);
  }
  printf("%d\n", ans);
  return 0;
}
