#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
char a[maxn][maxn];
int dp[105][maxn], dp1[105][maxn], n, m;
int main() {
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  memset(dp1, -1, sizeof dp1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= 2; k++)
      for (int j = m; j >= 1; j--) {
        if (a[i][j] - '0')
          dp[i][j] = 0;
        else if (j == m && dp[i][1] != -1)
          dp[i][j] = dp[i][1] + 1;
        else if (j != m && dp[i][j + 1] != -1)
          dp[i][j] = dp[i][j + 1] + 1;
      }
    for (int k = 1; k <= 2; k++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j] - '0')
          dp1[i][j] = 0;
        else if (j == 1 && dp1[i][m] != -1)
          dp1[i][1] = dp1[i][m] + 1;
        else if (j != 1 && dp1[i][j - 1] != -1)
          dp1[i][j] = dp1[i][j - 1] + 1;
      }
    }
  }
  int ans = (1 << 30);
  for (int j = 1; j <= m; j++) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      int r = -1;
      if (dp[i][j] != -1) r = dp[i][j];
      if (dp1[i][j] != -1 && dp1[i][j] < r) r = dp1[i][j];
      if (r == -1) {
        sum = -1;
        break;
      } else
        sum += r;
    }
    if (sum != -1) ans = min(ans, sum);
  }
  if (ans == (1 << 30))
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
