#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:50000000")
using namespace std;
const long long mod = 1000000007;
const char aa = 'a';
const double eps = 1E-9;
template <typename T>
bool within(T a, T b, T c) {
  return (a <= c && a >= b);
}
int gcd(int m, int n) {
  if (m == 0 || n == 0) return m + n;
  return gcd(n, m % n);
}
long long binom[30][30];
const int maxn = 40001;
const int dr[] = {0, -1, 0, 1};
const int dc[] = {1, 0, -1, 0};
void setBinom() {
  int Z = (int)sqrt(sizeof(binom) / sizeof(int) + eps);
  memset(binom, 0, sizeof(binom));
  for (int i = 0; i < (int)(Z); i++)
    for (int j = 0; j < (int)(Z); j++) {
      if (i && j)
        binom[i][j] = binom[i - 1][j] + binom[i][j - 1];
      else
        binom[i][j] = 1;
    }
}
vector<vector<int> > pos;
int n, m;
vector<string> B;
int rfind(int row, int col) {
  int N = pos[row].size();
  vector<int>& v = pos[row];
  int low = 0;
  int high = N;
  if (col > v[N - 1]) return v[0];
  int ans = -1;
  while (low < high) {
    int mid = (low + high) / 2;
    if (v[mid] >= col) {
      ans = v[mid];
      high = mid;
    } else
      low = mid + 1;
  }
  return ans;
}
int lfind(int row, int col) {
  int N = pos[row].size();
  vector<int>& v = pos[row];
  int low = 0;
  int high = N;
  if (col < v[0]) return v[N - 1];
  int ans = -1;
  while (low < high) {
    int mid = (low + high) / 2;
    if (v[mid] <= col) {
      ans = v[mid];
      low = mid + 1;
    } else
      high = mid;
  }
  return ans;
}
int cal() {
  cin >> n >> m;
  pos.resize(n);
  B.resize(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> B[i];
    for (int j = 0; j < (int)(m); j++)
      if (B[i][j] == '1') pos[i].push_back(j);
    if (pos[i].size() == 0) return -1;
  }
  int res = 1E9;
  for (int col = 0; col < (int)(m); col++) {
    int sum = 0;
    for (int row = 0; row < (int)(n); row++) {
      int rCol = rfind(row, col);
      int lCol = lfind(row, col);
      int drt = (rCol + m - col) % m;
      int dlt = (col + m - lCol) % m;
      sum += min(drt, dlt);
    }
    res = min(res, sum);
  }
  return res;
}
int main() {
  int res = cal();
  cout << res << endl;
}
