#include <bits/stdc++.h>
using namespace std;
int n, m, l[101][10001], r[101][10001], sol, sol1;
char a[101][10001];
int main() {
  cin >> n >> m;
  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++) l[i][0] = r[i][m + 1] = 0xFFFFFF;
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < 2; k++) {
      for (int j = 1; j <= m; j++)
        if (a[i][j] == '1')
          l[i][j] = 0;
        else
          l[i][j] = 1 + l[i][j - 1];
      l[i][0] = l[i][m];
      for (int j = m; j >= 1; j--)
        if (a[i][j] == '1')
          r[i][j] = 0;
        else
          r[i][j] = 1 + r[i][j + 1];
      r[i][m + 1] = r[i][1];
    }
  }
  sol = 0xFFFFFF;
  for (int j = 1; j <= m; j++) {
    sol1 = 0;
    for (int i = 1; i <= n; i++) sol1 += min(l[i][j], r[i][j]);
    sol = min(sol, sol1);
  }
  cout << (sol == 0xFFFFFF ? -1 : sol) << endl;
  return 0;
}
