#include <iostream>
#include <string>
#include <map>
#include <cassert>
using namespace std;

map<int,int> heap; // 
map<char,int> var; // value of variable
int hsize,maxhs,ridx,rvalue;
string in;

bool expr(int idx) {
    if(idx+1 < in.length() && in[idx+1] == '=') { // assignment
        bool inner = expr(idx+2);
        if(!inner) return inner;
        var[in[idx]] = rvalue;
        return true;
    }else if(in[idx] == 'm') {// malloc
        int s = 0,tidx = idx+7;
        while(isdigit(in[tidx])) {
            s *= 10;
            s += in[tidx] - '0';
            tidx++;
        }
        assert(in[tidx] == ')');

        if(hsize + s > maxhs) {// suff
            rvalue = -1;
            ridx = tidx+1;
            return true;
        }

        hsize += s;
        int k = heap.size();
        heap[k] = s;
        rvalue = k;
        ridx = tidx+1;
        return true;

    }else if(in[idx] == 'c') {// clone
        bool inner = expr(idx+6);
        assert(in[ridx] == ')');

        ridx++;
        if(inner == false) return false;
        if(rvalue == -2) {// clone(garbage)
            return false;
        }

        if(rvalue == -1) {// clone(NULL)
            return true;
        }

        if(heap.find(rvalue) == heap.end()) {
            return false;
        }

        int s = heap[rvalue];
        if(hsize+s > maxhs) {
            rvalue = -1;
            return true;
        }

        int k = heap.size();
        heap[k] = s;
        hsize += s;
        rvalue = k;
        return true;
    }else if(idx+1 < in.length() && in[idx] == 'N' && in[idx+1] == 'U') {// NULL
        ridx = idx+4;
        rvalue = -1;
        return true;
    }else if(in[idx] == '(') {
        bool inner = expr(idx+1);
        assert(in[ridx] == ')');
        ridx++;
        return inner;
    }else{ // variable
        ridx = idx+1;
        rvalue = var[in[idx]];
        return true;
    }
}

bool line() {
    if(in[0] == 'f'){// free
        bool inner = expr(5);
        assert(in[ridx] == ')');
        if(!inner) return inner;

        if(rvalue == -1) return true;
        if(heap.find(rvalue) == heap.end()) return false;
        hsize -= heap[rvalue];
        heap.erase(heap.find(rvalue));
        return true;
    }else{
        return expr(0);
    }
}

int main() {
    cin>>maxhs;

    heap.clear();
    var.clear();
    for(char c = 'A'; c <= 'Z'; ++c)
        var[c] = -2;

    bool haderror = false;
    while(cin>>in)
        if(!line()) haderror = true;

    if(haderror) {
        cout<<"Error"<<endl;
        return 0;
    }

    int ans = 0;
    for(map<int,int>::iterator it = heap.begin(); it != heap.end(); ++it) {
        if(it->second < 0) continue;
//        cout<<"search: "<<it->first<<","<<it->second<<endl;
        bool refed = false;
        for(map<char,int>::iterator it2=var.begin(); it2 != var.end(); ++it2) {
            if(it2->second == it->first) {
                refed = true;
                break;
            }
        }
        if(!refed) ans += it->second;
    }

    // for(map<char,int>::iterator it=var.begin(); it != var.end(); ++it)
    //     cout<<"var : "<<it->first<<","<<it->second<<endl;
    cout<<ans<<endl;

}

Measured memory_bytes: 0