Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

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


Output

0
/*
 * 2365.cc: Memory Leak
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

typedef vector<int> vi;
typedef vector<bool> vb;

/* global variables */

int m, vs[26];
int err = false;
vi hp;

/* subroutines */

int expr(string &s, int &pos);

int do_malloc(string &s, int &pos) {
  pos += 7; // 'malloc('
  int num = 0;
  while (pos < s.size() && s[pos] >= '0' && s[pos] <= '9')
    num = num * 10 + s[pos++] - '0';
  pos++;  // ')'

  if (m < num) return 0;
  m -= num;
  hp.push_back(num);
  return (hp.size() - 1);
}

int do_clone(string &s, int &pos) {
  pos += 6; // 'clone('
  int e = expr(s, pos);
  pos++; // ')'

  if (e == 0) return 0;
  if (e < 0 || hp[e] < 0) {
    err = true;
    return -1;
  }
  if (m < hp[e]) return 0;
  m -= hp[e];
  hp.push_back(hp[e]);
  return (hp.size() - 1);
}

int do_free(string &s, int &pos) {
  pos += 5; // 'free('
  int e = expr(s, pos);
  pos++; // ')'
  
  if (e == 0) return 0;
  if (e < 0 || hp[e] < 0) {
    err = true;
    return -1;
  }
  m += hp[e];
  hp[e] = -1;
  return 0;
}

int expr(string &s, int &pos) {
  if (s[pos] == '(') {
    pos++; // '('
    int e = expr(s, pos);
    pos++; // ')'
    return e;
  }

  if (pos + 4 <= s.size() && s[pos] == 'N' && s[pos + 1] == 'U') {
    pos += 4; // 'NULL'
    return 0;
  }

  if (s[pos] == 'm') return do_malloc(s, pos);
  if (s[pos] == 'c') return do_clone(s, pos);

  int vr = s[pos++] - 'A'; // [A-Z]

  if (pos < s.size() && s[pos] == '=') {
    pos++; // '='
    int e = expr(s, pos);
    vs[vr] = e;
  }

  return vs[vr];
}

int read_line(string &s) {
  int pos = 0;
  if (s[0] == 'f') return do_free(s, pos);
  return expr(s, pos);
}

/* main */

int main() {
  memset(vs, -1, sizeof(vs));
  hp.push_back(0);

  cin >> m;

  for (;;) {
    string s;
    cin >> s;
    if (cin.eof()) break;

    read_line(s);

    if (err) {
      puts("Error");
      return 0;
    }
  }

  vb refs(hp.size(), false);

  for (int i = 0; i < 26; i++)
    if (vs[i] > 0) refs[vs[i]] = true;

  int ans = 0;
  for (int i = 1; i < hp.size(); i++)
    if (hp[i] > 0 && ! refs[i]) ans += hp[i];
  printf("%d\n", ans);
  
  return 0;
}