import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Team
{
    private static class Pair<S extends Comparable<S>, T> implements Comparable<Pair<S, T>>
    {
	private S first;
	private T second;
	/* Class invariant: this.first != null && this.second != null */
	
	public Pair(S first, T second)
	{
	    super();
	    this.first = first;
	    this.second = second;
	}

	public S getFirst()
	{
	    return first;
	}

	public void setFirst(S first)
	{
	    this.first = first;
	}

	public T getSecond()
	{
	    return second;
	}

	public void setSecond(T second)
	{
	    this.second = second;
	}

	public boolean equals(Object object)
	{
	    boolean equal;
	    if (object != null && this.getClass() == object.getClass())
	    {
		Pair<S, T> other = (Pair<S, T>) object;
		equal = this.first.equals(other.first) && this.second.equals(other.second);
	    }
	    else 
	    {
		equal = false;
	    }
	    return equal;
	}
	
	public int compareTo(Pair<S, T> other)
	{
	    return this.first.compareTo(other.first);
	}
	
	public String toString()
	{
	    return "(" + this.first + ", " + this.second + ")";
	}
    }
    
    public static void main(String[] args)
    {
	PrintStream output = System.out;
	Scanner input = new Scanner(System.in);
	
	final int TEAM = 4;
	
	int number = input.nextInt();
	while (number != 0)
	{
	    Set<Pair<Integer, Set<Integer>>> runs = new TreeSet<Pair<Integer, Set<Integer>>>();
	    for (int i = 0; i < number; i++)
	    {
		Set<Integer> team = new HashSet<Integer>();
		for (int j = 0; j < TEAM; j++)
		{
		    team.add(input.nextInt());
		}
		runs.add(new Pair<Integer, Set<Integer>>(input.nextInt(), team));
	    }
	    
	    List<Pair<Integer, Set<Integer>>> times = new ArrayList<Pair<Integer, Set<Integer>>>(runs);
	    Collections.sort(times);
	    int minimum = Integer.MAX_VALUE;
	    for (int i = 0; i < times.size(); i++)
	    {
		for (int j = i + 1; j < times.size(); j++)
		{
		    Set<Integer> twoTeams = new HashSet<Integer>(times.get(i).getSecond());
		    twoTeams.addAll(new HashSet<Integer>(times.get(j).getSecond()));
		    if (twoTeams.size() == 2 * TEAM)
		    {
			int twoTimes = times.get(i).getFirst() + times.get(j).getFirst();
			if (twoTimes < minimum)
			{
			    minimum = twoTimes;
			    continue;
			}
		    }
		}
	    }
	    
	    if (minimum == Integer.MAX_VALUE)
	    {
		output.println(-1);
	    }
	    else
	    {
		output.println(minimum);
	    }
	    
	    number = input.nextInt();
	}
    }
}
