import java.util

import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {

  val n = readInt()
  val tickets = Array.tabulate(n){_ ⇒
    val Array(a, b, p) = readLine.trim.split(' ').map(_.toLong)
    if (a != 0 || b != 0) Ticket(a / gcd(a, b), b / gcd(a, b), p)
    else Ticket(0, 0, p)
  }.filterNot(t ⇒ t.a == 0 && t.b == 0).groupBy(_.vector).map(_._2.minBy{_.price}).toArray.sortBy(_.radian).toRing
  val min0 = new util.ArrayDeque[Ticket]().toDeque
  val min90 = new util.ArrayDeque[Ticket]().toDeque
  val min180 = new util.ArrayDeque[Ticket]().toDeque
  val min270 = new util.ArrayDeque[Ticket]().toDeque
  val min0to90 = new util.ArrayDeque[Ticket]().toDeque
  val min90to180 = new util.ArrayDeque[Ticket]().toDeque
  val min180to270 = new util.ArrayDeque[Ticket]().toDeque
  val min270to360 = new util.ArrayDeque[Ticket]().toDeque
  val in0 = mutable.Queue[Ticket]()
  val in90 = mutable.Queue[Ticket]()
  val in180 = mutable.Queue[Ticket]()
  val in270 = mutable.Queue[Ticket]()
  val all0to90 = mutable.Queue[Ticket]()
  val all90to180 = mutable.Queue[Ticket]()
  val all180to270 = mutable.Queue[Ticket]()
  val all270to360 = mutable.Queue[Ticket]()
  for (t ← tickets) {
    if (tickets.head.is0(t)) {
      in0.enqueue(t)
      min0.enqueue(t)
    }else if (tickets.head.in0to90(t)) {
      all0to90.enqueue(t)
      min0to90.eraseLast(_.price >= t.price)
      min0to90.enqueue(t)
    }else if (tickets.head.is90(t)){
      in90.enqueue(t)
      min90.enqueue(t)
    }else if (tickets.head.in90to180(t)) {
      all90to180.enqueue(t)
      min90to180.eraseLast(_.price >= t.price)
      min90to180.enqueue(t)
    }else if (tickets.head.is180(t)) {
      in180.enqueue(t)
      min180.enqueue(t)
    }else if (tickets.head.in180to270(t)) {
      all180to270.enqueue(t)
      min180to270.eraseLast(_.price >= t.price)
      min180to270.enqueue(t)
    }else if (tickets.head.is270(t)) {
      in270.enqueue(t)
      min270.enqueue(t)
    }else {
      all270to360.enqueue(t)
      min270to360.eraseLast(_.price >= t.price)
      min270to360.enqueue(t)
    }
  }
  var min = Long.MaxValue
  val queues = Array[(mutable.Queue[Ticket], Deque[Ticket], ((Ticket, Ticket) ⇒ Boolean))](
    (all270to360, min270to360, {(a, b) ⇒ a.in270to360(b)}),
    (in270, min270, {(a, b) ⇒ a.is270(b)}),
    (all180to270, min180to270, {(a, b) ⇒ a.in180to270(b)}),
    (in180, min180, {(a, b) ⇒ a.is180(b)}),
    (all90to180, min90to180, {(a, b) ⇒ a.in90to180(b)}),
    (in90, min90, {(a, b) ⇒ a.is90(b)}),
    (all0to90, min0to90, {(a, b) ⇒ a.in0to90(b)}),
    (in0, min0, {(a, b) ⇒ a.is0(b)})
  ).toRing
  for (t ← tickets) {
    for (i ← queues.indices) {
      while(queues(i - 1)._1.nonEmpty && !queues(i - 1)._3(t, queues(i - 1)._1.front)) {
        queues(i)._1.enqueue(queues(i - 1)._1.front)
        queues(i)._2.eraseLast(_.price >= queues(i - 1)._1.front.price)
        queues(i)._2.enqueue(queues(i - 1)._1.dequeue())
      }
      queues(i)._2.eraseFirst(!queues(i)._3(t, _))
    }
    if (min90to180.nonEmpty && min180to270.nonEmpty) {
      val temp: Long = t.price + min90to180.head.price + min180to270.head.price
      if (min > temp) {
        //println(temp, t, min90to180.head, min180to270.head)
        min = temp
      }
    }
    if (min90to180.nonEmpty && in270.nonEmpty) {
      val temp = t.price + min90to180.head.price + in270.front.price
      if (min > temp) {
        //println(temp, t, min90to180.head, in270.front)
        min = temp
      }
    }
    if (min180to270.nonEmpty && in90.nonEmpty) {
      val temp = t.price + in90.front.price + min180to270.head.price
      if (min > temp) {
        //println(temp, t, min180to270.headOption, in90.front)
        min = temp
      }
    }
    if (in180.nonEmpty) {
      val left = List(min0to90.headOption, in90.headOption, min90to180.headOption).flatMap(_.map(_.price)).minOption
      val right = List(min180to270.headOption, in270.headOption, min270to360.headOption).flatMap(_.map(_.price)).minOption
      if (left.isDefined && right.isDefined) {
        val temp = t.price + left.get + right.get + in180.front.price
        if (min > temp) {
          //println(temp, t, in180.front, min0to90.headOption, min90to180.headOption, min180to270.headOption, min270to360.headOption)
          min = temp
        }
      }
    }
  }
  println(if (min == Long.MaxValue) -1 else min)
  def gcd(a: Long, b: Long): Long = {
    @tailrec def rec(a: Long, b: Long): Long = {
      b match {
        case 0L ⇒ a
        case _ ⇒ rec(b, a % b)
      }
    }
    rec(a.abs, b.abs)
  }
  implicit class Wrap[T](val value: T) extends AnyVal {
    def toRing[A](implicit ev: T ⇒ Array[A]) = new RingBuffer(value)
    def toDeque[A](implicit ev: T ⇒ util.Deque[A]) = new Deque(value)
  }
  implicit class WrappedIterator[T](val value: List[T]) extends AnyVal {
    def minOption(implicit ev: Ordering[T]): Option[T] = if (value.nonEmpty) Some(value.min) else None
  }
}
class RingBuffer[T](private val array: Array[T]) extends Iterable[T]{
  override def iterator: Iterator[T] = array.iterator
  def apply(index: Int): T = array(((index % array.length) + array.length) % array.length)
  def update(index: Int, value: T): Unit = array(((index % array.length) + array.length) % array.length) = value
  def length: Int = array.length
  def indices = array.indices
}
class Deque[T](private val deque: util.Deque[T]) extends AnyVal {
  def eraseFirst(@inline predicate: T ⇒ Boolean): Unit = {
    while(!deque.isEmpty && predicate(deque.getFirst)) deque.pollFirst()
  }
  def eraseLast(@inline predicate: T ⇒ Boolean): Unit = {
    while(!deque.isEmpty && predicate(deque.getLast)) deque.pollLast()
  }
  def head: T = deque.getFirst
  def headOption: Option[T] = if (isEmpty) None else Some(head)
  def size: Int = deque.size()
  def enqueue(value: T): Unit = deque.addLast(value)
  def isEmpty: Boolean = deque.isEmpty
  def nonEmpty: Boolean = !isEmpty
}
case class Angle(sin: Double, cos: Double) {
  def radian: Double = {
    if (sin >= 0) math.acos(cos)
    else 2 * math.Pi - math.acos(cos)
  }
}
case class Vector(a: Long, b: Long) {
  def flip: Vector = Vector(-a, -b)
}
case class Ticket(a: Long, b: Long, price: Long) {
  def in0to90(that: Ticket): Boolean = cross(that) > 0 && dot(that) > 0
  def in90to180(that: Ticket): Boolean = cross(that) > 0 && dot(that) < 0
  def in180to270(that: Ticket): Boolean = cross(that) < 0 && dot(that) < 0
  def in270to360(that: Ticket): Boolean = cross(that) < 0 && dot(that) > 0
  def is0(that: Ticket): Boolean = cross(that) == 0 && dot(that) > 0
  def is90(that: Ticket): Boolean = cross(that) > 0 && dot(that) == 0
  def is180(that: Ticket): Boolean = cross(that) == 0 && dot(that) < 0
  def is270(that: Ticket): Boolean = cross(that) < 0 && dot(that) == 0
  def dot(that: Ticket): Long = a * that.a + b * that.b
  def cross(that: Ticket): Long = a * that.b - b * that.a
  def length: Double = math.sqrt(a * a + b * b)
  val radian = Angle(b / length, a / length).radian
  def vector: Vector = Vector(a, b)
}
