#include <bits/stdc++.h>
using namespace std;
int b[105][10005], i, j, n, m, sum, poz, Min = 10000000;
char a[105][10005], c;
bool f;
int main() {
  cin >> n >> m;
  scanf("%c", &c);
  for (i = 0; i < n; i++) {
    f = 0;
    for (j = 0; j < m; j++) {
      scanf("%c", &a[i][j]);
      if (a[i][j] == '1') f = 1;
    }
    scanf("%c", &c);
    if (!f) {
      cout << -1;
      return 0;
    }
  }
  for (i = 0; i < n; i++) {
    poz = -100005;
    for (j = 0; j < m; j++)
      if (a[i][j] == '1')
        poz = j;
      else
        b[i][j] = j - poz;
    poz = m - poz;
    for (j = 0; a[i][j] != '1'; j++) b[i][j] = j + poz;
  }
  for (i = 0; i < n; i++) {
    poz = 100005;
    for (j = m - 1; j >= 0; j--)
      if (a[i][j] == '1')
        poz = j;
      else
        b[i][j] = min(b[i][j], poz - j);
    for (j = m - 1; a[i][j] != '1'; j--) b[i][j] = min(b[i][j], poz + m - j);
  }
  for (j = 0; j < m; j++) {
    sum = 0;
    for (i = 0; i < n; i++) {
      sum += b[i][j];
    }
    if (Min > sum) {
      Min = sum;
    }
  }
  cout << Min;
  return 0;
}
