#include <bits/stdc++.h>
using namespace std;
int table[101][10001], mini[101][10001];
string line;
int main() {
  int n, m, ind, miniii;
  long long cnt;
  bool flag, err;
  while (cin >> n >> m) {
    err = false;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) mini[i][j] = 1000000;
    for (int i = 0; i < n; i++) {
      cin >> line;
      for (int j = 0; j < line.size(); j++) {
        table[i][j] = line[j] - '0';
      }
    }
    flag = false;
    for (int i = 0; i < n; i++) {
      flag = true;
      for (int j = 0; j < m; j++) {
        if (table[i][j] == 1) {
          flag = false;
          ind = j;
          break;
        }
      }
      if (flag) {
        cout << -1 << endl;
        err = true;
        break;
      }
      for (int j = 0; j < m; j++)
        if (table[i][j] == 1) mini[i][j] = 0;
      mini[i][ind] = 0;
      for (int j = (ind + 1) % m; j != ind; j = (j + 1) % m) {
        mini[i][j] = min(mini[i][j], mini[i][(j - 1 + m) % m] + 1);
      }
      for (int j = (ind - 1 + m) % m; j != ind; j = (j - 1 + m) % m) {
        mini[i][j] = min(mini[i][j], mini[i][(j + 1) % m] + 1);
      }
    }
    if (!err) {
      miniii = 1000000;
      for (int i = 0; i < m; i++) {
        cnt = 0;
        for (int j = 0; j < n; j++) {
          cnt += mini[j][i];
        }
        if (cnt < miniii) miniii = cnt;
      }
      cout << miniii << endl;
    }
  }
  return 0;
}
