#include <bits/stdc++.h>
using namespace std;
long long *arr1, *arr2;
long long cost[101][10001];
bool calc_cost(int i, vector<int> &v, int m) {
  int last_one = -1;
  for (int k = 0; k < m; ++k) {
    if (v[k] == 0 && last_one == -1)
      arr1[k] = -1;
    else {
      if (v[k] == 1) {
        arr1[k] = 0;
        last_one = 1;
      } else {
        arr1[k] = last_one;
        last_one++;
      }
    }
  }
  int last_cost = arr1[m - 1] + 1;
  if (arr1[m - 1] == -1) return false;
  for (int k = 0; arr1[k] != 0; k++) {
    if (arr1[k] == -1) {
      arr1[k] = last_cost;
      last_cost++;
    } else
      break;
  }
  last_one = -1;
  for (int k = m - 1; k >= 0; k--) {
    if (v[k] == 0 && last_one == -1)
      arr2[k] = -1;
    else {
      if (v[k] == 1) {
        arr2[k] = 0;
        last_one = 1;
      } else {
        arr2[k] = last_one;
        last_one++;
      }
    }
  }
  last_cost = arr2[0] + 1;
  if (arr2[0] == -1) return false;
  for (int k = m - 1; arr2[k] != 0; k--) {
    if (arr2[k] == -1) {
      arr2[k] = last_cost;
      last_cost++;
    } else
      break;
  }
  for (int k = 0; k < m; k++) {
    if (arr1[k] <= arr2[k])
      cost[i][k] = arr1[k];
    else
      cost[i][k] = arr2[k];
  }
  return true;
}
int main() {
  int n, m;
  char x;
  cin >> n >> m;
  vector<int> vec[n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; ++j) {
      cin >> x;
      vec[i].push_back(x - 48);
    }
  }
  arr1 = new long long int[m];
  arr2 = new long long int[m];
  for (int i = 0; i < n; ++i) {
    bool f = false;
    f = calc_cost(i, vec[i], m);
    if (f == false) {
      cout << "-1" << endl;
      return 0;
    }
  }
  long long mincost = -1;
  for (int i = 0; i < m; i++) {
    long long tempcost = 0;
    for (int j = 0; j < n; ++j) {
      tempcost += cost[j][i];
    }
    if (tempcost < mincost || mincost == -1) mincost = tempcost;
  }
  cout << mincost << endl;
  return 0;
}
