While valuation is far from the only thing that matters, long-term investors can give themselves an edge by focusing on high-quality stocks that trade at attractive valuations. The best company bought at the wrong price can make for a terrible investment.

Two stocks that look like particularly good deals right now are Ally Financial (ALLY -1.56%) and International Business Machines (IBM -8.25%). Both have solid growth prospects, and both trade at beaten-down prices.

Ally Financial

The banking industry has gone through some turmoil this year. The high-profile failures of Silicon Valley Bank and Signature Bank in March spooked investors, and rightfully so. While both banks had uniquely risky strategies, even the strongest bank can succumb to an out-of-control bank run.

Shares of Ally Financial were hit as all of this was unfolding, suffering a steep drop in March. While the banking crisis may not be entirely over, Ally is having no trouble holding onto deposits. In fact, Ally reported solid customer and deposit growth during the first quarter.

Ally gained 126 thousand net new deposit customers in the first quarter, a record for the bank. Retail deposits increased by $813 million to $138.5 billion, and 91% of those deposits are FDIC insured. Ally now has 2.8 million retail deposit customers with an average balance of $50,000. The company did see an outflow of uninsured deposits, but that was more than offset by inflows of insured deposits.

Ally does face its fair share of risks. The bank's assets are concentrated in auto loans, and as the economy continues to take a toll on consumers, the prospect of increased losses on those loans is a real possibility. Ally has diversified into mortgages, credit cards, personal loans, and corporate finance, but those lines of business are still small relative to the core auto business.

If you can accept the risks associated with Ally's auto loan portfolio, the stock looks like a great deal. Ally stock trades below its tangible book value, and with the company expecting adjusted earnings per share of $3.65 this year, the price-to-earnings ratio is less than 8. Ally did lower its EPS guidance from $4, and the company may very well miss its new target if the economic environment deteriorates further. But there's a big margin of safety built in.

Bank stocks may seem risky in the wake of the banking crisis earlier this year. But Ally looks like a bargain stock worthy of being bought and held.

International Business Machines

As companies look for productivity gains and cost efficiencies in a tough economy, IBM is well positioned to benefit. After nearly a decade of divestitures, investments, and transformation, IBM is now squarely focused on helping its clients improve their operations through hybrid cloud computing and artificial intelligence.

Around 75% of IBM's revenue in the first quarter came from software and consulting, and more than half of all revenue was recurring in nature. OpenShift, a containerization platform offered by Red Hat, is now at a $1 billion annual recurring revenue run rate. The consulting business grew by 8% in the first quarter, and that growth is helping to drive software sales in IBM's portfolio.

IBM recently unveiled its new watsonx AI platform aimed at helping enterprise customers train, validate, and deploy AI models while ensuring that those models meet regulatory and compliance requirements. While a scrappy start-up may not have to worry about AI running amok, highly regulated industries are a different story. IBM's AI platform can enable enterprises to adopt cutting-edge AI technologies while minimizing risks.

While IBM isn't a fast-growing company, it does expect to expand revenue by as much as 5% this year, excluding the impact of currency. Free cash flow should come in around $10.5 billion, according to the company's guidance, up about $1 billion from last year. IBM stock currently trades for a bit more than 11 times this free cash flow guidance. Given the potential for long-term growth driven by hybrid cloud computing and AI, that looks like a good deal to me.