#include <bits/stdc++.h>
using namespace std;
string floors[200];
int cnt[200];
int dp[200][2];
long long ttl = 0;
int solve(int f, int l, int t) {
  int x = cnt[f];
  int ans1 = 0, ans2 = 0, ans = 0;
  if (l == 0) {
    for (int j = 0; j < floors[f].size(); j++) {
      if (x == 0) {
        if (f == 0) break;
        if (t == cnt[f]) break;
        ans1 += (floors[f].size() - (j)) + solve(f - 1, 1, t - cnt[f]) + ans;
        ans2 += j + 1 + solve(f - 1, 0, t - cnt[f]) + ans;
        break;
      }
      if (floors[f][j] == '1') x--;
      if (x == 0) {
        if (f == 0) break;
        if (t == cnt[f]) break;
        ans1 += (floors[f].size() - (j)) + solve(f - 1, 1, t - cnt[f]) + ans;
        ans2 += j + 1 + solve(f - 1, 0, t - cnt[f]) + ans;
        break;
      }
      ans++;
    }
  } else {
    for (int j = floors[f].size() - 1; j >= 0; j--) {
      if (x == 0) {
        if (f == 0) break;
        if (t == cnt[f]) break;
        ans1 += (floors[f].size() - (j)) + solve(f - 1, 1, t - cnt[f]) + ans;
        ans2 += j + 1 + solve(f - 1, 0, t - cnt[f]) + ans;
        break;
      }
      if (floors[f][j] == '1') x--;
      if (x == 0) {
        if (f == 0) break;
        if (t == cnt[f]) break;
        ans1 += (floors[f].size() - (j)) + solve(f - 1, 1, t - cnt[f]) + ans;
        ans2 += j + 1 + solve(f - 1, 0, t - cnt[f]) + ans;
        break;
      }
      ans++;
    }
  }
  if (f == 0 || t == cnt[f]) {
    ans1 += ans;
    ans2 += ans;
  }
  dp[f][l] = min(ans1, ans2);
  return dp[f][l];
}
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> floors[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < floors[i].size(); j++) {
      if (floors[i][j] == '1') {
        cnt[i]++;
        ttl++;
      }
    }
  }
  ans = solve(n - 1, 0, ttl);
  cout << ans << endl;
}
