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 <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

const int NOT_ASIGNED = -1;
const int FREE = -2;
int n = 1;
int m;
int pos;
int sum = 0;
char str[1000];
int variable[200];
int pointer[10000];

int Expr();
int Malloc(int s) {
  if (sum + s > m) { return 0; }
  pointer[n++] = s;
  sum += s;
  return -(n - 1);
}

int Clone(int p) {
  if (p == 0) { return 0; }
  if (p > 0 || pointer[-p] == NOT_ASIGNED || pointer[-p] == FREE) { throw 1; }
  return Malloc(pointer[-p]);
}

void Free(int p) {
  if (p == 0) { return; }
  if (p > 0 || pointer[-p] == NOT_ASIGNED || pointer[-p] == FREE) { throw 1; }
  sum -= pointer[-p];
  pointer[-p] = FREE;
}

int Number() {
  int ret = 0;
  while (isdigit(str[pos])) {
    ret = ret * 10 + (str[pos] - '0');
    pos++;
  }
  return ret;
}

int Expr() {
  int lhs;
  if (str[pos] == '(') {
    pos++;
    lhs = Expr();
    assert(str[pos] == ')');
    pos++;
  } else if (str[pos] == 'N' && str[pos + 1] == 'U') {
    lhs = 0;
    pos += 4;
  } else if (str[pos] == 'm') {
    pos += 7;
    int s = Number();
    lhs = Malloc(s);
    assert(str[pos] == ')');
    pos++;
  } else if (str[pos] == 'c') {
    pos += 6;
    int p = Expr();
    lhs = Clone(p);
    assert(str[pos] == ')');
    pos++;
  } else {
    assert(isupper(str[pos]));
    int p = str[pos] - 'A';
    pos++;
    if (str[pos] == '=') {
      pos++;
      int rhs = Expr();
      variable[p] = rhs;
    }
    lhs = variable[p];
  }
  return lhs;
}

void Line() {
  if (str[pos] == 'f') {
    pos += 5;
    int p = Expr();
    Free(p);
    assert(str[pos] == ')');
    pos++;
  } else {
    Expr();
  }
}

int main() {
  MEMSET(variable, 0x0f);
  MEMSET(pointer, -1);
  pointer[0] = 0;
  scanf("%d", &m);
  try {
    fgets(str, 999, stdin);
    while (fgets(str, 999, stdin)) {
      pos = 0;
      Line();
    }
    REP(i, 200) {
      if (variable[i] <= 0) { pointer[-variable[i]] = 0; }
    }
    int ans = 0;
    REP(i, n) {
      if (pointer[i] == FREE) { continue; }
      ans += pointer[i];
    }
    printf("%d\n", ans);
  } catch (int x) {
    puts("Error");
  }
}