Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

100
A=malloc(10)
B=clone(A)
free(A)


Output

0
#include<iostream>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fr(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(),c.end()

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef long long ll;
const int inf=(int)1e9;
const double EPS=1e-9, INF=1e12;

int sz[100000],m;
bool released[100000];
map<string,int> v;
int mem,use;
string in;
bool err;

int rec(int l,int r){
	//cerr<<in.substr(l,r-l)<<endl;
	int i,d=0;
	for(i=l;i<r;i++){
		if(in[i]=='(')d++;
		if(in[i]==')')d--;
		if(d==0&&in[i]=='=')break;
	}
	if(i<r){
		return v[in.substr(l,i-l)]=rec(i+1,r);
	}
	d=0;
	for(i=r-1;i>=l;i--){
		if(in[i]==')')d++;
		if(in[i]=='(')d--;
		if(d==0)break;
	}
	if(i==r-1)return v[in.substr(l,r-l)];
	if(i==l)return rec(i+1,r-1);
	
	string op=in.substr(l,i-l);
	if(op=="malloc"){
		int s=atoi(in.substr(i+1,r-i-2).c_str());
		if(s<=mem-use){
			sz[m]=s;
			use+=s;
			return m++;
		}
		return -1;
	}
	if(op=="free"){
		int s=rec(i+1,r-1);
		if(s<0)return -1;
		if(s==0||released[s])err=1;
		else released[s]=1, use-=sz[s];
		return 0;
	}
	if(op=="clone"){
		int s=rec(i+1,r-1);
		if(s<0)return -1;
		if(s==0||released[s])return err=1, 0;
		if(sz[s]<=mem-use){
			sz[m]=sz[s];
			use+=sz[s];
			return m++;
		}
		return -1;
	}
	return v[in.substr(l,r-l)];
}
int main(){
	cin>>mem;
	m=1;
	v["NULL"]=-1;
	err=0;
	while(cin>>in){
		rec(0,in.size());
	}
	if(err)cout<<"Error"<<endl;
	else{
		fr(i,v)if(i->second>0&&!released[i->second]){
			released[i->second]=1;
			use-=sz[i->second];
		}
		cout<<use<<endl;
	}
	return 0;
	
}