#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    map<ll, int> nums;
    rep(i, n) {
        cin >> a[i];
        nums[a[i]]++;
    }

    sort(a.rbegin(), a.rend());
    int ans = 0, i = 0;
    while (i < n) {
        ll biggest_num = a[i++];
        if (nums[biggest_num] == 0) {
            continue;
        }
        nums[biggest_num]--;
        ll sumToBe = 0;
        while ((biggest_num >> sumToBe) > 0ll) {
            sumToBe++;
        }

        ll comp = (1 << sumToBe) - biggest_num;
        if (nums[comp]) {
            ans++;
            nums[comp]--;
        }
    }
    cout << ans << endl;
}

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template<class _T> static void getint(_T& a) {
	const char* fmt = " %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld";
	}

	if (scanf(fmt, &a) < 0) {
		printf("g1int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b) {
	const char* fmt = " %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld";
	}

	if (scanf(fmt, &a, &b) < 0) {
		printf("g2int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b, _T& c) {
	const char* fmt = " %d %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld %lld";
	}

	if (scanf(fmt, &a, &b, &c) < 0) {
		printf("g3int Error\n");
	}
}

static void AGC005A();


#if 1
int main()
{
	AGC005A();
	fflush(stdout);
	return 0;
}
#endif

static void AGC005A()
{
	std::string orgs;
	std::cin >> orgs;

	int sCount = 0, eraseCount = 0;
	int len = orgs.length();
	for (int i = 0; i < len; i++) {
		if (orgs[i] == 'S') {
			sCount++;
		}
		else {
			if (sCount) {
				sCount--;
				eraseCount += 2;
			}
		}
	}
	printf("%d\n", len - eraseCount);
}
