<s>[INST] This problem is from an interactive coding challenge, so I don't have a lot of information about the input. All I know is that I have to write a function `reorderedPowerOf2` that takes an integer `N` as input and returns the smallest integer `M` which is a power of 2 that can be obtained by reordering the digits of `N`. If no such integer exists, return -1.

Here is my code:

```python
def isPowerOf2(n):
    return (n != 0) and (n & (n-1)) == 0

def reorderedPowerOf2(N):
    count = {}
    for char in str(N):
        if char in count:
            count[char] += 1
        else:
            count[char] = 1
    num = 1
    while num < 10**9:
        if isPowerOf2(num):
            count2 = {}
            for char in str(num):
                if char in count2:
                    count2[char] += 1
                else:
                    count2[char] = 1
            if count == count2:
                return num
        num *= 2
    return -1
```

This code works, but it's slow. It generates all powers of 2 up to 10^9 and checks whether the digit count matches. This takes a long time. I am looking for a way to improve the performance.

Can you help?[/INST]