#include <iostream>
using namespace std;
int main(){
int score = 0, t;
int pre = 0;
while(cin >> t && t){
if(t == 1) pre = 1, score += t;
else{
if(pre == 1) pre = 2, score += 2;
else pre = pre + 2, score += pre;
}
}
cout << score << endl;
return 0;
}