#include <bits/stdc++.h>
using namespace std;
#ifdef SG
#include <debug.h>
#else
#define show(...)
#define debug(...)
#define deepen(...)
#define timer(...)
#endif
#define ARG4(_1,_2,_3,_4,...) _4
#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)
#define forn2(i,n) forn3 (i, 0, n)
#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)
#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)
#define ford2(i,n) ford3 (i, 0, n)
#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)
#define ve vector
#define pa pair
#define tu tuple
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fs first
#define sc second
#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)(a).size())
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned char uc;
typedef pa<int, int> pii;
typedef pa<int, ll> pil;
typedef pa<ll, int> pli;
typedef pa<ll, ll> pll;
typedef ve<int> vi;
const ld pi = 3.1415926535897932384626433832795l;
template<typename T> inline auto sqr (T x) -> decltype(x * x) {return x * x;}
template<typename T1, typename T2> inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
template<typename T1, typename T2> inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}
const int N = 32;
const int M = 32;
struct Input {
int m;
ll q[M];
ui mskl[M], mskr[M];
bool read () {
int n;
if (!(cin >> n >> m)) {
return 0;
}
forn (i, m) {
string s;
cin >> s >> q[i];
mskl[i] = ~0u;
mskr[i] = 0u;
forn (j, n) {
mskl[i] -= 1ull << j;
mskl[i] += ui(s[j] == '0' || s[j] == '?') << j;
mskr[i] += ui(s[j] == '1' || s[j] == '?') << j;
}
}
return 1;
}
void init (const Input &input) {
*this = input;
}
};
struct Data: Input {
ll ans;
void write () {
printf("%" PRId64 "\n", ans);
}
virtual void solve () {}
virtual void clear () {
*this = Data();
}
};
struct Solution: Data {
ll calc (int k, ui l, ui r, int cnt) {
if (!k) {
return 0;
}
ui l1 = l & mskl[k - 1];
ui r1 = r & mskr[k - 1];
if (~(l1 | r1)) {
return calc(k - 1, l, r, cnt);
}
int cnt1 = __builtin_popcount(l1 & r1);
ll res = q[k - 1] << cnt1;
if (cnt1 == cnt) {
return res;
}
if (cnt1 == cnt - 1) {
return res + calc(k - 1, r1 ^ l ^ r, l1 ^ l ^ r, cnt - 1);
}
return res + calc(k - 1, l, r, cnt) - calc(k - 1, l1, r1, cnt1);
}
void solve () {
timer();
ans = calc(m, ~0u, ~0u, 32);
}
void clear () {
}
};
Solution sol;
int main () {
cout.setf(ios::showpoint | ios::fixed);
cout.precision(20);
#ifdef SG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
while (sol.read()) {
sol.solve();
sol.write();
sol.clear();
}
#else
// freopen("", "r", stdin);
// freopen("", "w", stdout);
sol.read();
sol.solve();
sol.write();
#endif
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgU0cKCSNpbmNsdWRlIDxkZWJ1Zy5oPgojZWxzZQoJI2RlZmluZSBzaG93KC4uLikKCSNkZWZpbmUgZGVidWcoLi4uKQoJI2RlZmluZSBkZWVwZW4oLi4uKQoJI2RlZmluZSB0aW1lciguLi4pCiNlbmRpZgoKI2RlZmluZSBBUkc0KF8xLF8yLF8zLF80LC4uLikgXzQKCiNkZWZpbmUgZm9ybjMoaSxsLHIpIGZvciAoaW50IGkgPSBpbnQobCk7IGkgPCBpbnQocik7ICsraSkKI2RlZmluZSBmb3JuMihpLG4pIGZvcm4zIChpLCAwLCBuKQojZGVmaW5lIGZvcm4oLi4uKSBBUkc0KF9fVkFfQVJHU19fLCBmb3JuMywgZm9ybjIpIChfX1ZBX0FSR1NfXykKCiNkZWZpbmUgZm9yZDMoaSxsLHIpIGZvciAoaW50IGkgPSBpbnQocikgLSAxOyBpID49IGludChsKTsgLS1pKQ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