#include <bits/stdc++.h>
using namespace std;
const int MAX = 10101;
int dist[MAX];
int dp[101][MAX], bo[101][MAX];
void bfs(vector<int> b, int row, int col) {
  queue<int> q;
  for (int i = (0); i < (col + 5); ++i) dist[i] = -1;
  for (int i = (0); i < ((int)b.size()); ++i) {
    if (b[i]) {
      q.push(i);
      dist[i] = 0;
    }
  }
  while (!q.empty()) {
    int fr = q.front();
    q.pop();
    if (fr >= col || fr < 0) continue;
    if (fr == 0) {
      if (col != 1 && dist[fr + 1] == -1) {
        dist[fr + 1] = dist[fr] + 1;
        q.push(fr + 1);
      }
      if (dist[col - 1] == -1) {
        dist[col - 1] = dist[fr] + 1;
        q.push(col - 1);
      }
    } else if (fr == col - 1) {
      if (col != 1 && dist[fr - 1] == -1) {
        dist[fr - 1] = dist[fr] + 1;
        q.push(fr - 1);
      }
      if (dist[0] == -1) {
        dist[0] = dist[fr] + 1;
        q.push(0);
      }
    } else {
      if (dist[fr + 1] == -1) {
        dist[fr + 1] = dist[fr] + 1;
        q.push(fr + 1);
      }
      if (dist[fr - 1] == -1) {
        dist[fr - 1] = dist[fr] + 1;
        q.push(fr - 1);
      }
    }
  }
  for (int i = (0); i < (col); ++i) dp[row][i] = dist[i];
}
int main() {
  int n, m;
  cin >> n >> m;
  bool ok = true;
  for (int i = (0); i < (n); ++i) {
    int cnt = 0;
    string st;
    cin >> st;
    for (int j = (0); j < (m); ++j) {
      if (st[j] == '1')
        bo[i][j] = 1;
      else
        bo[i][j] = 0;
      if (bo[i][j]) cnt++;
    }
    if (!cnt) ok = false;
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = (0); i < (n); ++i) {
    vector<int> b;
    for (int j = (0); j < (m); ++j) b.push_back(bo[i][j]);
    bfs(b, i, m);
  }
  int ans = (1 << 28);
  for (int i = (0); i < (m); ++i) {
    int sum = 0;
    for (int j = (0); j < (n); ++j) {
      sum += dp[j][i];
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
