#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
const int M = 128;
const int inf = 0x7fffffff;
const long long INF = 9E18;
const int mod = 1e9 + 7;
int n, m;
int pos[M][maxn], len[M];
int dp[M][maxn];
char s[M][maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '1') pos[i][len[i]++] = j;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (len[i] == 0) {
        puts("-1");
        return 0;
      }
      int cnt = len[i];
      int loc = lower_bound(pos[i], pos[i] + cnt, j) - pos[i];
      int movl = inf, movr = inf;
      if (loc == len[i]) {
        loc--;
        movr = j - pos[i][loc];
        movl = m - (j - pos[i][0]);
      } else if (loc == 0) {
        movl = pos[i][loc] - j;
        movr = m - (pos[i][len[i] - 1] - j);
      } else {
        movl = pos[i][loc] - j;
        movr = j - pos[i][--loc];
      }
      dp[i][j] = min(movl, movr);
    }
  }
  int ans = inf;
  for (int i = 1; i <= m; i++) {
    int sum = 0;
    for (int j = 1; j <= n; j++) {
      sum += dp[j][i];
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
