#include <bits/stdc++.h>
using namespace std;
int dist[104][10004], dp[104][10004];
vector<int> v;
int main() {
  bool isAns = true;
  int n, m;
  scanf("%d%d", &n, &m);
  cin.get();
  for (int i = 0; i < n; ++i) {
    v.clear();
    bool isOne = false;
    for (int j = 0; j < m; ++j) {
      char c;
      scanf("%c", &c);
      if (c == '1') {
        isOne = true;
        v.push_back(j);
        v.push_back(j + m);
        v.push_back(j + 2 * m);
      }
    }
    if (isAns) isAns = isOne;
    if (isAns == true) {
      sort(v.begin(), v.end());
      for (int j = 0; j < m; ++j) {
        int pos = (int)(lower_bound(v.begin(), v.end(), j + m) - v.begin());
        dist[i + 1][j + 1] = min(j + m - v[pos - 1], v[pos] - j - m);
      }
    }
    cin.get();
  }
  for (int i = 1; i < n + 1; ++i) {
    for (int j = 1; j < m + 1; ++j) {
      dp[i][j] = dp[i - 1][j] + dist[i][j];
    }
  }
  if (isAns == false)
    printf("-1\n");
  else {
    int result = INT_MAX;
    for (int j = 1; j < m + 1; ++j) result = min(result, dp[n][j]);
    printf("%d\n", result);
  }
  return 0;
}
