fun main(args: Array<String>?): Unit {
    val m = readLine()!!.trim().toInt()
    val state = State(m)
    while(true){
        val line = readLine() ?: break
        state.execute {
            Program.parseLine(line)()
        }
    }
    println(when{
        state.isCrashed -> "Error"
        else -> state.leakedMemory()
    })
}


typealias Expression = State.Allocator.() -> Reference
typealias Statement = State.Allocator.() -> Unit

object Program {

    fun parseLine(line: String): Statement {
        return parseExpr(line, 0)?.second?.toStatement() ?: parseFree(line, 0)!!.second
    }
    private fun Expression.toStatement(): Statement {
        return {this@toStatement() }
    }
    private fun <F, R> (State.Allocator.() -> F).andThen(func: State.Allocator.(from: F) -> R): State.Allocator.() -> R {
        return { func(this@andThen()) }
    }
    private fun <R> pure(value: R): State.Allocator.() -> R {
        return {value}
    }

    private fun <F, M, R> ((F) -> M).transform(func: (M) -> R): (F) -> R {
        return { func(this(it)) }
    }

    private fun parseNumber(line: String, position: Int): Pair<Int, Int>? {
        return ((position..line.lastIndex).firstOrNull { !line[it].isDigit() }
                ?: line.length).takeIf { it != position }?.let { idx ->
            Pair(idx, (position until idx).fold(0) { a, b -> a * 10 + (line[b] - '0') })
        }
    }

    private fun parseVariable(line: String, position: Int): Pair<Int, Char>? {
        return if (position in line.indices && line[position].isUpperCase() && (position + 1 !in line.indices || !line[position + 1].isUpperCase())) Pair(position + 1, line[position])
        else null
    }
    private fun parseVariableReference(line: String, position: Int): Pair<Int, Expression>? {
        return parseVariable(line, position)?.let{(pos, char) -> Pair(pos, pure(char).andThen(State.Allocator::variable))}
    }

    private fun parseNull(line: String, position: Int): Pair<Int, Expression>? {
        return if (line.startsWith("NULL", position)) Pair<Int, Expression>(position + 4, pure(Reference.Nullptr)) else null
    }

    private fun parseExpr(line: String, position: Int): Pair<Int, Expression>? {
        return if (position in line.indices) {
            when(line[position]) {
                '(' -> parseExpr(line, position + 1)?.let{it.copy(first = it.first + 1)}
                else -> parseAssign(line, position) ?: parseNull(line, position) ?: parseVariableReference(line, position) ?: parseMalloc(line, position) ?: parseClone(line, position)
            }
        }else null
    }

    private fun parseClone(line: String, position: Int): Pair<Int, Expression>? {
        return if (line.startsWith("clone(", position))
            parseExpr(line, position + 6)?.let { (endPos, expression) ->
                if (line[endPos] == ')')
                    Pair(endPos + 1, expression.andThen(State.Allocator::clone))
                else null
            }
        else null
    }

    private fun parseAssign(line: String, position: Int): Pair<Int, Expression>? {
        return parseVariable(line, position)?.let { (pos, char) ->
            if (line.getOrNull(pos) == '=') parseExpr(line, pos + 1)?.let { (endPos, expression) ->
                Pair(endPos, expression.transform { char to it }.andThen(State.Allocator::assign))
            } else null
        }
    }

    private fun parseFree(line: String, position: Int): Pair<Int, Statement>? {
        return if (line.startsWith("free(", position))
            parseExpr(line, position + 5)?.let { (endPos, expression) ->
                if (line[endPos] == ')')
                    Pair(endPos + 1, expression.andThen(State.Allocator::free))
                else null
            } else null
    }

    private fun parseMalloc(line: String, position: Int): Pair<Int, Expression>? {
        return if (line.startsWith("malloc(", position))
            parseNumber(line, position + 7)?.let { (endPos, size) ->
                if (line[endPos] == ')')
                    Pair<Int, Expression>(endPos + 1, pure(size).andThen(State.Allocator::malloc))
                else null
            } else null
    }
}

sealed class Reference {
    object Nullptr : Reference()
    object Uninitialized : Reference()
    data class Pointer(val address: Int, val size: Int) : Reference()
}

class State(capacity: Int) {
    private val allocator: Allocator = Allocator(capacity)
    fun execute(statement: Allocator.() -> Unit): State {
        if (!allocator.isCrashed) {
            statement(allocator)
        }
        return this
    }
    val isCrashed: Boolean
        get() = allocator.isCrashed
    fun leakedMemory(): Int = allocator.calLeaked()

    class Allocator(private val capacity: Int) {
        var isCrashed = false
            private set
        fun calLeaked(): Int {
            return used - stack.values.mapNotNull {
                when(it){
                    is Reference.Pointer -> it
                    else -> null
                }
            }.distinctBy { it.address }.filter{ it.address in heap }.sumBy { it.size }
        }
        private val heap: MutableSet<Int> = mutableSetOf()
        private var unusedAddress = 0
        private val stack: MutableMap<Char, Reference> = mutableMapOf()
        private var used = 0
        fun malloc(size: Int): Reference {
            return when {
                isCrashed || used + size > capacity -> Reference.Nullptr
                else -> {
                    used += size
                    unusedAddress++.let { address ->
                        heap += address
                        Reference.Pointer(address, size)
                    }
                }
            }
        }

        fun free(reference: Reference): Unit {
            if (isCrashed) return
            when (reference) {
                is Reference.Nullptr -> Unit
                is Reference.Uninitialized -> isCrashed = true
                is Reference.Pointer -> {
                    if (heap.remove(reference.address)) {
                        used -= reference.size
                    } else {
                        isCrashed = true
                    }
                }
            }
        }

        fun clone(reference: Reference): Reference {
            if (isCrashed) return Reference.Nullptr
            return when (reference) {
                is Reference.Nullptr -> Reference.Nullptr
                is Reference.Uninitialized -> {
                    isCrashed = true
                    Reference.Nullptr
                }
                is Reference.Pointer -> malloc(reference.size)
            }
        }

        fun variable(char: Char): Reference {
            return stack[char]?.takeIf{it !is Reference.Pointer || it.address in heap} ?: Reference.Uninitialized
        }

        fun assign(pair: Pair<Char, Reference>): Reference {
            stack[pair.first] = pair.second
            return pair.second
        }
    }
}

inline fun <T> T.apply(func: T.() -> Unit): T {
    func(this)
    return this
}
