from sys import stdin

def getval():
    n,m = map(int,input().split())
    ab = []
    c = []
    def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
    for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
    return n,m,ab,c 

def main(n,m,ab,c):
    dp = [-1 for i in range(2**n)]
    dp[0] = 0

    for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
    print(dp[-1])
        
                
if __name__=="__main__":
    n,m,ab,c = getval()
    main(n,m,ab,c) from sys import stdin def getval():
    n,m = map(int,input().split())
    ab = []
    c = []
    def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
    for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
    return n,m,ab,c  getval n,m = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split m ab = [] ab [] c = [] c [] def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
     tobit idx = len(arr)-1 idx len(arr)-1 len(arr) len arr 1 s = "" s "" for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
         i range(n-1,-1,-1) range n-1 n 1 -1 -1 if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
         arr[idx]-1==i and idx!=-1 arr[idx]-1==i arr[idx]-1 arr[idx] arr idx 1 i idx!=-1 idx -1 s = "".join([s,"1"]) s "".join([s,"1"]) "".join "" join [s,"1"] s "1" idx -= 1 idx 1 s = "".join([s,"0"]) s "".join([s,"0"]) "".join "" join [s,"0"] s "0" return s s arr arr for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
     i range(m) range m ab.append(list(map(int,stdin.readline().split()))) ab.append ab append list(map(int,stdin.readline().split())) list map(int,stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split c.append(tobit(list(map(int,stdin.readline().split())))) c.append c append tobit(list(map(int,stdin.readline().split()))) tobit list(map(int,stdin.readline().split())) list map(int,stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split return n,m,ab,c return n,m,ab,c n m ab c def main(n,m,ab,c):
    dp = [-1 for i in range(2**n)]
    dp[0] = 0

    for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
    print(dp[-1])
        
                 main -1 for i in range(2**n) for i in range(2**n) i range(2**n) range 2**n 2 n for i in range(2**n) -1 dp = [-1 for i in range(2**n)] dp [-1 for i in range(2**n)] dp[0] = 0 dp[0] dp 0 0 for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
     i range(m) range m curc = ab[i][0] curc ab[i][0] [i] ab i 0 curk = c[i] curk c[i] c i -1 for i in range(2**n) for i in range(2**n) i range(2**n) range 2**n 2 n for i in range(2**n) -1 temp = [-1 for i in range(2**n)] temp [-1 for i in range(2**n)] for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
         j range(2**n) range 2**n 2 n if dp[j]==-1:
                continue
             dp[j]==-1 dp[j] dp j -1 continue new = dp[j] new dp[j] dp j update = int(curk,2)|j update int(curk,2)|j int(curk,2) int curk 2 j target = temp[update] target temp[update] temp update if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
             target==-1 target -1 temp[update] = new+curc temp[update] temp update new+curc new curc elif target>new+curc:
                temp[update] = new+curc                
             target>new+curc target new+curc new curc temp[update] = new+curc temp[update] temp update new+curc new curc if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
         new<temp[j] or temp[j]==-1 new<temp[j] new temp[j] temp j temp[j]==-1 temp[j] temp j -1 temp[j] = dp[j] temp[j] temp j dp[j] dp j dp = temp dp temp print(dp[-1]) print dp[-1] dp -1 n n m m ab ab c c if __name__=="__main__":
    n,m,ab,c = getval()
    main(n,m,ab,c) __name__=="__main__" __name__ "__main__" n,m,ab,c = getval() n getval() getval m ab c main(n,m,ab,c) main n m ab c n,m,ab,c = getval() getval() n def getval():
    n,m = map(int,input().split())
    ab = []
    c = []
    def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
    for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
    return n,m,ab,c  def getval():
    n,m = map(int,input().split())
    ab = []
    c = []
    def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
    for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
    return n,m,ab,c  getval m,ab,c = getval() getval() m def main(n,m,ab,c):
    dp = [-1 for i in range(2**n)]
    dp[0] = 0

    for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
    print(dp[-1])
        
                 def main(n,m,ab,c):
    dp = [-1 for i in range(2**n)]
    dp[0] = 0

    for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
    print(dp[-1])
        
                 main ab,c = getval() getval() ab c = getval() getval() c
from sys import stdin

def getval():
    n,m = map(int,input().split())
    ab = []
    c = []
    def tobit(arr):
        idx = len(arr)-1
        s = ""
        for i in range(n-1,-1,-1):
            if arr[idx]-1==i and idx!=-1:
                s = "".join([s,"1"])
                idx -= 1
            else:
                s = "".join([s,"0"])
        return s
    for i in range(m):
        ab.append(list(map(int,stdin.readline().split())))
        c.append(tobit(list(map(int,stdin.readline().split()))))
    return n,m,ab,c 

def main(n,m,ab,c):
    dp = [-1 for i in range(2**n)]
    dp[0] = 0

    for i in range(m):
        curc = ab[i][0]
        curk = c[i]
        temp = [-1 for i in range(2**n)]
        for j in range(2**n):
            if dp[j]==-1:
                continue
            new = dp[j]
            
            update = int(curk,2)|j
            
            #print(i,j,update,new,curc,curk,s)
            target = temp[update]
            if target==-1:
                temp[update] = new+curc
            elif target>new+curc:
                temp[update] = new+curc                
            if new<temp[j] or temp[j]==-1:
                temp[j] = dp[j]
        dp = temp
        #print(dp)
    print(dp[-1])
        
                
if __name__=="__main__":
    n,m,ab,c = getval()
    main(n,m,ab,c)