module mod_queue
  implicit none

  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node

  type t_queue
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: enqueue => enqueue
    procedure :: dequeue => dequeue
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type t_queue

contains

  subroutine finalize(this)
    type(t_queue), intent(inout) :: this

    call clear(this)
  end

  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node

    allocate(node)
    node%item = item
  end

  subroutine enqueue(this,item)
    class(t_queue), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node

    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end

  function dequeue(this) result(item)
    class(t_queue), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node

    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end

  function peek(this) result(item)
    class(t_queue), intent(in) :: this
    integer :: item

    item = this%head%item
  end

  subroutine clear(this)
    class(t_queue), intent(inout) :: this
    type(t_node), pointer :: node, next

    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end

  integer function size_of(this)
    class(t_queue), intent(in) :: this

    size_of = this%num
  end

end module mod_queue
use mod_queue
integer N,M,b
integer,allocatable::reader(:)
integer,allocatable::a(:)
logical,allocatable::Key(:,:)
logical,allocatable::Box(:)
type(t_queue),allocatable::keys(:)
read*,N,M
allocate(Key(M,N),a(M),keys(N),box(N))

Key=.false.
do i=1,M
  read*,a(i),b
  allocate(reader(B))
  read*,reader
  do j=1,B
    Key(i,reader(j))=.true.
    call enqueue(keys(reader(j)),i)
  end do
  deallocate(reader)
end do
BoX=.false.
if(DFS(BOX,1,N)/=10**9)then
  print"(i0)",DFS(BOX,1,N)
else
  print"(i0)",-1
endif
contains
recursive function DFS(BOX,BOXNUM,BOXes)result(res)
  logical::BOX(:)
  integer BOXNUM,tmpkey,BOXes
  integer::res
  integer sizebox
  logical,allocatable::tmp(:)
  sizebox=size(box)
  allocate(tmp(sizeBox))
  res=10**9
  if(BOX(BOXNUM))then
    res=DFS(BOX,BOXNUM,BOXes)
  else
    do while(size_of(keys(BOXNUM))/=0)
      tmpkey=dequeue(keys(BOXNUM))
      tmp=BOX
      do i=1,BOXes
        tmp(i)=tmp(i).or.key(tmpkey,i)
      end do
      if(BOXNUM/=BOXes)then
        res=min(res,a(tmpkey)+DFS(tmp,BOxnum+1,Boxes))
      else
        res=A(tmpkey)
      endif
    end do
  endif
end function

end