#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

vector<int> vec;
vector<bool> flag;
map<char, int> mp;
int M;
int sum;
bool dame;
typedef pair<int, const char*> parsed;

parsed line(const char *p);
parsed expr(const char *p);

parsed line(const char *p) {
  if (*p == 'f') {              // free
    parsed r = expr(p+5);
    //cout << "free " << r.first << endl;
    if (r.first == -2) dame = 1;
    else if (r.first == -1) ;
    else {
      if (flag[r.first]) dame = 1;
      flag[r.first] = 1;
      sum -= vec[r.first];
    }
    return parsed(-3, r.second);
  } else {                      // expr
    parsed r = expr(p);
    //cout << "expr " << r.first << endl;
    return parsed(r.first, r.second);
  }
}
parsed expr(const char *p) {
  if (*p == '(') {
    parsed r = expr(p+1);
    return parsed(r.first, r.second+1);
  } else if (isupper(*p) && *(p+1)=='=') {     // assign
    parsed r = expr(p+2);
    //cout << "assign " << *p << " = " << r.first << endl;
    mp[*p] = r.first;
    return r;
  } else if (*p == 'N' && *(p+1) == 'U') { // null
    //cout << "null" << endl;
    return parsed(-1, p+4);
  } else if (isupper(*p)) {     // variable
    int res = -2;
    if (mp.count(*p)) res = mp[*p];
    //cout << "variable " << *p << endl;
    return parsed(res, p+1);
  } else if (*p == 'm') {       // malloc
    p += 7;
    int t = *(p++)-'0';
    while(isdigit(*p)) t = t*10+*(p++)-'0';
    int res = -1;
    if (sum + t <= M) {
      vec.push_back(t);
      flag.push_back(0);
      res = vec.size()-1;
      sum += t;
    }
    //cout << "malloc " << t << endl;
    return parsed(res, p+1);
  } else if (*p == 'c') {       // clone
    p += 6;
    parsed r = expr(p);
    //cout << "clone " << r.first << endl;
    int res = -1;
    if (r.first == -2) dame = 1;
    else if (r.first == -1) ;
    else {
      if (flag[r.first]) dame = 1;
      else {
        if (sum + vec[r.first] <= M) {
          vec.push_back(vec[r.first]);
          flag.push_back(0);
          sum += vec[r.first];
          res = vec.size()-1;
        }
      }
    }
    return parsed(res, p+1);
  }
}

int main() {
  cin >> M;
  string input;
  sum = 0;
  dame = 0;
  while(cin >> input) {
    if (input == "#") break;
    line(input.c_str());
    // cout << ":";
    // REP(i, vec.size()) cout << vec[i] << " " ;cout << endl;
    // REP(i, vec.size()) cout << flag[i] << " " ;cout << endl;
    // cout << endl;
  }
  int ans = 0;
  FOR(it, mp) {
    if (it->second >= 0)
      flag[it->second] = 1;
  }
  REP(i, vec.size()) {
    if (flag[i] == 0) {
      ans += vec[i];
    }
  }
  if (dame)
    cout << "Error" << endl;
  else
    cout << ans << endl;
}

Measured memory_bytes: 0