Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

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


Output

0
#include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem2365 : Memory Leak **/
const int null = -1, error = -2, nin = -3;
string s;
int p;

int ptr, size, maxSize;

vector<int> mem_size;
vector<bool> mem_flg;
int var[26];

int number(), malloc(), expr(), assign(), clone();
bool free(), line();

int number()
{
	int res = 0;
	while (isdigit(s[p])) {
		res*=10;
		res+=s[p]-'0';
		p++;
	}
	return res;
}

int malloc()
{
	p+=7;
	int b = number();
	p++;
	
	if (size+b>maxSize)
		return null;
	
	mem_size.push_back(b);
	mem_flg.push_back(true);
	size += b;
	
	return ptr++;
}

int clone()
{
	p+=6;
	int a = expr();
	if (a==null)
		return null;
	else if (a == error || a == nin || a >= mem_size.size() || !mem_flg[a]) {
		return error;
	}
	
	if (size+mem_size[a]>maxSize)
		return null;
	
	mem_size.push_back(mem_size[a]);
	mem_flg.push_back(true);
	size += mem_size[a];
	
	return ptr++;
}

int assign()
{
	int v = s[p] - 'A';
	p+=2;
	return var[v] = expr();
}

int expr()
{
	if (s[p] == '(') {
		p++;
		int t = expr();
		p++;
		return t;
	} else if (s[p] == 'N' && s[p+1] == 'U') {
		p+=4;
		return null;
	} else if (s[p] == 'm') {
		return malloc();
	} else if (s[p] == 'c') {
		return clone();
	} else if ('A'<=s[p] && s[p]<='Z' && s[p+1]=='=') {
		return assign();
	} else {
		return var[s[p++]-'A'];
	}
}


bool free()
{
	p+=5;
	int a = expr();
	p++;
	
	if (a == null) {
		return true;
	}
	
	
	if (a == nin || a == error || a >= mem_size.size() || !mem_flg[a]) {
		return false;
	}
	
	
	mem_flg[a] = false;
	size -= mem_size[a];
	mem_size[a] = 0;
	
	return true;
}

bool line()
{
	if (s[p] == 'f') {
		return free();
	} else {
		return expr()!=error;
	}
}


int main()
{
	cin>>maxSize;
	ptr=0, size=0;
	fill(var, var+26, nin);
	
	bool eFlg = true;
	while (cin>>s) {
		p=0;
		eFlg &= line();
	}
	
	if (!eFlg)
		cout << "Error" << endl;
	else {
		int ans = 0;
		rep(i, mem_size.size()) {
			if (mem_flg[i]) {
				bool flg=false;
				rep(j, 26) {
					if (i == var[j])
						flg = true;
				}
				if (!flg)
					ans+=mem_size[i];
			}
		}
		cout << ans << endl;
	}
}