
const
	MaxN = 100010;

var
	inFile, outFile : text;
	n, differentNum, i, left, right, ind : longint;
	Kind, Num : array[0..3] of longint;
	a : array[0..MaxN] of longint;
	sol : int64;

// da li postoji data vrsta bombona medju trenutnim
function find(x : longint) : longint;
begin
	find := -1;
	for i := 1 to 3 do
		if (x = Kind[i]) then find := i;
end;

// naci slobodno mesto da ubacimo trenutnu vrstu
function freeIndex : longint;
begin
	for i := 1 to 3 do
		if (Num[i] = 0) then freeIndex := i;
end;

BEGIN

	assign(inFile, 'bombone.in');
	assign(outFile, 'bombone.out');
	reset(inFile); rewrite(outFile);

	read(inFile, n);
	for i := 1 to n do read(inFile, a[i]);

	differentNum := 0;
	for i := 1 to 3 do begin
		Kind[i] := 0;
		Num[i] := 0;
	end;

	sol := 0;
	left := 1;
	for right := 1 to n do begin

		ind := find(a[right]); // da li vrsta a[right] vec postoji
		if (ind <> -1) then Num[ind] := Num[ind] + 1 // ako da, povecati brojac
		else begin
			if (differentNum = 3) then // ako ne postoji i imamo 3 razlicite vrste, moramo da izbacujemo
				while (differentNum = 3) do begin
					ind := find(a[left]);
					Num[ind] := Num[ind] - 1;
					left := left + 1;
					if (Num[ind] = 0) then begin
						Kind[ind] := 0;
						differentNum := differentNum - 1;
					end;
				end;

			ind := freeIndex;
			Kind[ind] := a[right];
			Num[ind] := 1;
			differentNum := differentNum + 1;
		end;

		sol := sol + (right - left + 1);
	end;

	writeln(outFile, sol);
	close(inFile);
	close(outFile);

END.
