<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _matches_contigs(in_file, contigs, checked_file):
 """Check if the contigs in the input file match the defined contigs in the reference genome. """

    tocheck_contigs = 2
    if utils.file_exists(checked_file):
        with open(checked_file) as in_handle:
            return in_handle.read().strip() == "match"
    else:
        with utils.open_gzipsafe(in_file) as in_handle:
            to_check = set([])
            for line in in_handle:
                if not line.startswith("#"):
                    to_check.add(line.split()[0])
                if len(to_check) >= tocheck_contigs:
                    break
        with open(checked_file, "w") as out_handle:
            if any([c not in contigs for c in to_check]):
                out_handle.write("different")
                return False
            else:
                out_handle.write("match")
                return True