#include<iostream>
using namespace std;
int main()
{
int i,sum = 0;
int st;
int k = 1;
while(cin>>i,i)
{
if(i==1)
{
sum++;
st = true;
}
else if(i==2)
{
if(st)
{
k = 1;
st = false;
}
sum = sum+2*k;
k++;
}
}
cout<<sum;
return 0;
}