#include <bits/stdc++.h>
using namespace std;
int n, m;
int d[100][10000];
bool a[10000];
int main() {
  cin >> n >> m;
  bool impossible = false;
  for (int i = 0; i < n; ++i) {
    bool found = false;
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) {
      a[j] = s[j] == '1';
      if (a[j]) found = true;
    }
    if (!found) {
      impossible = true;
      break;
    }
    for (int j = 0; j < m; ++j) d[i][j] = -1;
    int dist;
    dist = -100000;
    for (int j = 0, times = 2; times; dist++) {
      if (a[j]) dist = 0;
      if (dist >= 0 && (d[i][j] == -1 || dist < d[i][j])) d[i][j] = dist;
      if (++j == m) {
        j = 0;
        times--;
      }
    }
    dist = -100000;
    for (int j = m - 1, times = 2; times; dist++) {
      if (a[j]) dist = 0;
      if (dist >= 0 && (d[i][j] == -1 || dist < d[i][j])) d[i][j] = dist;
      if (--j == -1) {
        j = m - 1;
        times--;
      }
    }
  }
  int best = -1;
  if (!impossible)
    for (int i = 0; i < m; ++i) {
      int sum = 0;
      for (int j = 0; j < n; ++j) {
        sum += d[j][i];
      }
      if (best == -1 || sum < best) best = sum;
    }
  cout << best << endl;
  return 0;
}
