#include <iostream>
using namespace std;

int main() {
	long n, c, res = 0, num = 0; //num - количество двуслотовых электростанций.
	long temp;  //temp - количество свободных слотов.
	cin >> n;
	temp = n;
	char s;
	while(cin >> s) {
		c = (s - '0');
		num += (c == 2) ? 1 : 0;
		if (temp - c < 0 && num >= 1) {
			temp += (2 - c);
			num -= 1;
		}
		else if (temp - c >= 0) {
			temp -= c;
		}
		res += (num < 1) ? (n - temp) : (n - temp - num);
	}
	cout << res;
	return 0;
}