kostil algorithm