data class Clock(var hours: Int, var minutes: Int) {
    init {
        val minutesTemp = minutes.rem(60)
        var extraHours = minutes.div(60)
        if (minutesTemp < 0){
            extraHours--
            this.minutes = 60 + minutesTemp
        } else this.minutes = minutesTemp

        val hoursTemp = (hours + extraHours).rem(24)
        this.hours = if (hoursTemp < 0){ 24 + hoursTemp } else hoursTemp
    }

    private fun addMinutes(x: Int): Unit {
        if ((x + this.minutes) >= 60) {
            addHours((x + this.minutes).div(60))
            this.minutes = (x + this.minutes).rem(60)
        } else {
            this.minutes += x
        }

        while (this.minutes < 0){
            this.minutes += 60
            this.hours -= 1
        }

        this.hours = if (this.hours.rem(24) < 0) { 24 + this.hours.rem(24) } else this.hours.rem(24)
    }

    private fun addHours(x: Int): Unit {
        if (x + this.hours > 23){
            this.hours = (x+this.hours).rem(24)
        } else {
            this.hours += x
        }
    }

    fun add(i: Int) {
        this.addMinutes(i)
    }

    override fun toString(): String {
        return "${if (hours > 9){ "$hours"} else {"0$hours"}}:${if (minutes > 9){ "$minutes"} else {"0$minutes"}}"
    }
}
