#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7;
void OUT(long double o, int x) {
  cout << fixed << setprecision(x) << o;
  return;
}
long long b[100], c[100], d[100], n, m;
string a[100];
long long l = -1, ans = INF;
void bt(int x, string s) {
  if (x == l) {
    long long z = l;
    for (int i = 0; i < x; i++) {
      if (s[i] == '0' && s[i + 1] == '0')
        z += 2 * (m + 1 - d[i]);
      else if (s[i] == '1' && s[i + 1] == '1')
        z += 2 * (m + 1 - c[i]);
      else
        z += m + 1;
    }
    if (s[x] == '0')
      z += m + 1 - d[x];
    else
      z += m + 1 - c[x];
    ans = min(ans, z);
    return;
  }
  bt(x + 1, s + "0");
  bt(x + 1, s + "1");
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = n - 1; i >= 0; i--) cin >> a[i];
  for (int i = 0; i < n; i++) {
    bool z = 0;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '1' && !z) {
        c[i] = j;
        z = 1;
      }
      if (a[i][j] == '1') d[i] = m + 1 - j;
    }
    if (z) {
      b[i] = 1;
      l = i;
    } else
      d[i] = m + 1, c[i] = m + 1;
  }
  if (l == -1) return cout << 0, 0;
  bt(0, "0");
  cout << ans;
  return 0;
}
