#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <vector>
#include <map>

using namespace std;

#define dump(n) cerr<<"# "<<#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)

struct Parsed{
	int index,id;
	Parsed(){}
	Parsed(int index,int id):index(index),id(id){}
};
struct Area{
	bool valid;
	int size;
	Area(){}
	Area(bool valid,int size):valid(valid),size(size){}
};

int memory;
map<char,int> toid;   // name -> id
map<int,Area> toarea; // id -> (valid flg,size)

int remain_memory()
{
	int size=0;
	foreach(i,toarea)
		size+=i->second.size;
	return memory-size;
}

Parsed parse_expr(const string& s,int index);

Parsed parse_variable(const string& s,int index)
{
	//printf("parse_variable(s,%d)\n",index);
	assert(isupper(s[index]));
	
	char var=s[index];
	if(toid.find(var)==toid.end())
		toid[var]=-1;
	return Parsed(index+1,toid[var]);
}
Parsed parse_assign(const string& s,int index)
{
	//printf("parse_assign(s,%d)\n",index);
	assert(isupper(s[index]) && index+1<s.size() && s[index+1]=='=');
	
	char var=s[index];
	Parsed p=parse_expr(s,index+2);
	toid[var]=p.id;
	
	return p;
}
Parsed parse_malloc(const string& s,int index)
{
	//printf("parse_malloc(s,%d)\n",index);
	assert(s[index]=='m');
	
	index+=7;
	
	int size=0;
	while(isdigit(s[index]))
		size=size*10+s[index++]-'0';
	
	if(remain_memory()<size)
		return Parsed(index+1,0);
	
	int id=toarea.size();
	Area area(true,size);
	toarea.insert(make_pair(id,area));
	return Parsed(index+1,id);
}
Parsed parse_clone(const string& s,int index)
{
	//printf("parse_clone(s,%d)\n",index);
	assert(s[index]=='c');
	
	Parsed p=parse_expr(s,index+6);
	if(p.id==-1 || !toarea[p.id].valid)
		throw "Error(parse_clone)";
	if(p.id==0)
		return Parsed(p.index+1,0);
	else{
		int id=toarea.size();
		Area area(true,toarea[p.id].size);
		if(remain_memory()>=area.size){
			toarea.insert(make_pair(id,area));
			return Parsed(p.index+1,id);
		}
		else
			return Parsed(index+1,0);
	}
}

Parsed parse_free(const string& s,int index)
{
	//printf("parse_expr(s,%d)\n",index);
	assert(s[index]=='f');
	Parsed p=parse_expr(s,index+5);
	
	if(p.id==-1 || !toarea[p.id].valid)
		throw "Error(parse_free)";
	if(p.id)
		toarea[p.id]=Area(false,0);
	
	return Parsed(p.index+1,-1);
}

Parsed parse_expr(const string& s,int index)
{
	//printf("parse_expr(s,%d)\n",index);
	assert(index<s.size());
	
	if(s[index]=='('){
		Parsed p=parse_expr(s,index+1);
		return Parsed(p.index+1,p.id);
	}
	else if(s.substr(index,4)=="NULL")
		return Parsed(index+4,0);
	else if(isupper(s[index])){
		if(index+1<s.size() && s[index+1]=='=')
			return parse_assign(s,index);
		else
			return parse_variable(s,index);
	}
	else if(s[index]=='m')
		return parse_malloc(s,index);
	else if(s[index]=='c')
		return parse_clone(s,index);
	else if(s[index]=='f')
		return parse_free(s,index);
	
	assert(false);
}

void parse_line(const string& s)
{
	if(s[0]=='f')
		parse_free(s,0);
	else
		parse_expr(s,0);
}

int main()
{
	while(cin>>memory && memory!=-1){
		cin.ignore();
		vector<string> lines;
		for(string s;getline(cin,s),s.size();)
			lines.push_back(s);
		
		//dump(memory);
		//rep(i,lines.size())
		//	dump(lines[i]);
		
		toid.clear();
		toarea.clear();
		toarea[0]=Area(true,0);
		
		try{
			rep(i,lines.size())
				parse_line(lines[i]);
			//puts("toid:");
			//foreach(i,toid)
			//	printf("name=%c id=%d\n",i->first,i->second);
			//puts("toarea:");
			//foreach(i,toarea)
			//	printf("id=%d valid=%d size=%d\n",i->first,i->second.valid,i->second.size);
			
			foreach(i,toid)
				toarea[i->second].size=0;
			int res=0;
			foreach(i,toarea)
				if(i->second.valid)
					res+=i->second.size;
			
			cout<<res<<endl;
		}
		catch(const char* message){
			//cout<<message<<endl;
			cout<<"Error"<<endl;
		}
	}
	return 0;
}

Measured memory_bytes: 0