#include <iostream>
using namespace std;
const int N = 30;
int q[N];
int main(){
int x,k = -1,res = 0;
while(cin >> x)
if(x > 0) q[++k] = x;
int flag = 1;
for(int i = 0;i <= k;i++){
if(q[i] == 2){
if(flag == 1) res += q[i];
else res += q[i] * flag;
flag++;
}else if(q[i] == 1){
flag = 1;
res += 1;
}
}
cout << res << endl;
return 0;
}