#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vvi field(5000,vi(5));
		vi top(5);
		rep(k,n){
			int d,p,q; cin>>d>>p>>q;
			if(d==1){
				int t=*max_element(top.begin()+q-1,top.begin()+q-1+p);
				rep(i,p){
					field[t][q-1+i]=1;
					top[q-1+i]=t+1;
				}
			}
			else{
				rep(i,p)
					field[top[q-1]+i][q-1]=1;
				top[q-1]+=p;
			}
			
			int cnt=0;
			rep(i,field.size()){
				if(count(allof(field[i]),1)<5)
					field[i-cnt]=field[i];
				else
					cnt++;
			}
			
			repi(i,*max_element(allof(top))-cnt,field.size())
				field[i]=vi(5);
			fill(allof(top),0);
			rep(i,field.size())
				rep(j,5)
					if(field[i][j])
						top[j]=i+1;
		}
		int res=0;
		rep(i,field.size())
			res+=count(allof(field[i]),1)%5;
		cout<<res<<endl;
	}
	
	return 0;
}