#include<iostream>
using namespace std;
int main()
{
int n, g = 0, cnt1 =1, cnt2=1;
cin >> n;
while(n != 0)
{
if(n == 1) cnt2 = 1, cnt1 = 1;
if(n == 2)
{
if(cnt1 == 1)
{
cnt2 = 2, cnt1 = 2;
}
else if(cnt1 == 2) cnt2 += 2;
}
g += cnt2;
cin >> n;
}
cout << g << endl;
return 0;
}