#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include <cstring>
#include<cmath>
using namespace std;
int count_ = 0;
int sum = 0;
bool flag = true;
int main()
{
while (flag)
{
int x;
cin >> x;
switch (x)
{
case 1:
count_++;
sum = 0;
break;
case 2:
sum += 2;
count_ += sum;
break;
case 0:
flag = false;
break;
}
}
cout << count_;
return 0;
}