void main() {
  problem();
}

void problem() {
  auto N = scan!int;
  auto X = scan!string.map!(c => c == '0' ? 0 : 1).array;

  int[] toBitArray(long n) {
    enum MAX_DIVIDER_BITS = 14;
    auto divider = new int[MAX_DIVIDER_BITS];
    int dividerBitSize;
    for(int i = 1, o = MAX_DIVIDER_BITS - 1; i < 2^^MAX_DIVIDER_BITS; i <<= 1, o--) {
      if ((n & i) == i) {
        divider[o] = 1;
        dividerBitSize = MAX_DIVIDER_BITS - o;
      }
    }
    return divider[$-dividerBitSize..$];
  }

  void solve() {
    X[0] ^= 1;

    X.deb;
    auto bitSize = N - X.countUntil!(x => x == 1);
    auto divider = toBitArray(bitSize);
    
    auto Xo = X.dup;
    const int dl = cast(int)divider.length;
    int carry;
    int offset;
    deb(divider);
    
    loop:foreach(_; 0..100) {
      auto Xd = Xo.dup;
      if (offset > 0 && Xd[offset - 1] == 1) carry = 1;

      foreach(int d; 0..dl) {
        int i = d + offset;
        if (i >= bitSize) break loop;
        
        Xd[i] -= divider[d];

        if (Xd[i] == -1) {
          foreach(c; 1..N) {
            auto xdi = i - c;
            if (xdi == -1) {
              offset += 1;
              continue loop;
            }

            Xd[xdi] ^= 1;
            if (Xd[xdi] == 0) break;
          }
          Xd[i] = 1;
        }

        if (Xd[i] == 0 && carry != 0) carry++;
        if (Xd[i] == 1) carry = 1;
      }

      Xo = Xd;
      offset = cast(int)Xo.countUntil!(x => x == 1);
      deb(Xo);
    }
  }

  solve();
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Queue = Tuple!(long, "from", long, "to");

// -----------------------------------------------
