#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e15 + 7;
const ll N = 1e5 + 5;
int righ[102][N], lef[102][N];
void solve() {
  int n, m;
  cin >> n >> m;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int c = -1;
    for (int j = 0; j < m; j++) {
      int num = s[j] - '0';
      if (num)
        righ[i][j] = j, c = j;
      else
        righ[i][j] = c;
    }
    c = -1;
    for (int j = m - 1; j >= 0; j--) {
      int num = s[j] - '0';
      if (num)
        lef[i][j] = j, c = j;
      else
        lef[i][j] = c;
    }
  }
  int ans = INT_MAX;
  for (int j = 0; j < m; j++) {
    int t = 0;
    for (int i = 0; i < n; i++) {
      int l = righ[i][j], r = lef[i][j];
      if (l != -1 && r != -1)
        t += min(j - l, r - j);
      else if (r == -1 && l != -1) {
        t += min(j - l, lef[i][0] + m - j);
      } else if (l == -1 && r != -1) {
        t += min(r - j, m - righ[i][m - 1] + j);
      } else if (l == -1 && r == -1) {
        cout << -1;
        return;
      }
    }
    ans = min(ans, t);
  }
  cout << ans;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
